0

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>
#include<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;   
}
ildjarn
  • 62,044
  • 9
  • 127
  • 211
aki
  • 155
  • 3
  • 6
  • 17
  • 6
    Make sure that you have [a good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c++-book-guide-and-list). What, exactly, do you think that `new int[3,i+N,i+N+1,i+1]` does? Why are you using raw dynamically allocated arrays at all? – James McNellis Apr 13 '11 at 01:31
  • i want to insert data whole data of array into vector of a map without using for loops. is it possible to push back data set into vector of map? – aki Apr 13 '11 at 01:38

1 Answers1

5
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.

Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • And `delete[] temp;` should be within `if (n==0 && m==5){...}` block. – a1ex07 Apr 13 '11 at 01:53
  • please don't care about the things inside my array – aki Apr 13 '11 at 02:04
  • 1
    That doesn't really matter. Even if you assigned valid values to the elements in the array, your code is still wholly nonsensical. – James McNellis Apr 13 '11 at 02:16
  • yes i am not good in programming so help me to slove this problem. i have 2 data sets M,N, so need 2 loops. in each step of this loop i will get a set of values(34, 6, 12,0) i want to store these outputs in a map for further processing.so output will be: key 1, vector(34, 6, 12,0); key 2 vector(32, 16, 2)....so on that why i used a map with vector.in each step i will get a set of values and need to put these into a vector of my map. i can use pushback only for 1 value so any possibility do this? help me...if u think that this is nonsensical so help me to correct this.... – aki Apr 13 '11 at 09:44
  • @aki: You need to get a good introductory book like one of those listed in the post linked from my answer. _Programming: Principles and Practice Using C++_ is highly recommended by many people as a good introductory text, especially if you don't have much programming experience. I still don't understand what you are trying to do, but regardless you need to back up a bit and start with some of the fundamentals. – James McNellis Apr 13 '11 at 17:14
  • @James, thanks for your advice, but i need a qucik access for this question that's why i posted my question here, the codes which i wrote here are not completed one. within my for loops, i should write codes for some calulation based on my equation that's part i can do so at the beginig i used it as a 3,i+N,i+N+1,i+1 (for example) because in end of each loop i will get a result similar to this that's mean a value set(2,20, 12..)so before strat next step of my loop i should append this result into my map..i think my question is not a very difficult to answer. – aki Apr 14 '11 at 00:36
  • you should give me hint or some example to do this without saying to read book.anyway,i solved it and working it properly – aki Apr 14 '11 at 00:36