开发者

pushback data from vector to vector of map

i have created a map called select_p and vector of this map is called pts. i have stored data in a array and i want to pushbcak these data into my vector of map. i tried this by inserting value of array into new vector and then pushback into my map.but it is not working please help me to correct these codes? thanks

#include<iostream>
#in开发者_如何学编程clude<cstdlib>
#include <map>
#include <vector>

using namespace std;
int main()
{
    int M=7;
    int N=6;
    int i=0;
    int * temp;
    map<int,vector<int> > select_p;
    vector<int>pts;

    for (int m=0; m<M; m++)
    {
        for (int n=0; n<N; n++)
        {
            vector<int>id;
            if (n==0 && m==5)
            {
               temp = new int[3,i+N,i+N+1,i+1];
               unsigned ArraySize = sizeof(temp) / sizeof(int);
               id.insert(id.begin(),temp[0], temp[ArraySize]);
               select_p[i].push_back(id);
            }
            i++;
        } 
    }
    delete[] temp;

    system("PAUSE");   
    return 0;   
}


for (int m=0; m<M; m++) {
    for (int n=0; n<N; n++) {
        if (n==0 && m==5) {

Why are you looping when you only actually do anything for a single pair of values of m and n? The loops are completely useless here; you would get the same effect by just setting n = 0 and m = 5.

temp = new int[3,i+N,i+N+1,i+1];

Whatever you think this does, that's not what it does. This is equivalent to temp = new int[i+1];. The rest of the expression inside of the [] has no effect.

That said, you should not use new to create arrays in your program. Use std::vector; it is far easier to use correctly.

unsigned ArraySize = sizeof(temp) / sizeof(int);

This does not work. When you dynamically allocate an array, you are responsible for keeping track of how many elements are in it. Given a pointer to a dynamically allocated array (like temp here) there is no way to determine the number of elements in the array.

What you have is equivalent to sizeof(int*) / sizeof(int), which is not going to do what you expect.

id.insert(id.begin(),temp[0], temp[ArraySize]);

std::vector::insert takes a range of iterators: you have provided it with two values. Presumably you want to use temp, which points to the initial element of the dynamically allocated array, and temp + i + 1, which points one past the end of the array. That said, since you haven't set the values of the elements in the array, you are copying uninitialized memory, which probably isn't what you mean to do.

select_p[i].push_back(id);

select_p[i] is a std::vector<int>. std::vector<int>::push_back() takes a single int that is appended to the sequence. Presumably you just mean to use assignment to assign id to select_p[i].

You should get a good introductory C++ book if you want to learn to program in C++. I am sorry to say that your program is nonsensical.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜