0

I have a 2d vector

typedef vector <double> record_t;
typedef vector <record_t> data_t;
data_t data;

So my 2d vector is data here. It has elements like say,

1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

Now I want to insert these elements into another 2d vector

 std::vector< vector<double> > window;

So what I did was to create an iterator for traversing through the rows of data and pushing it into window like

std::vector< std::vector<double> >::iterator data_it;

    for (data_it = data.begin() ; data_it != data.end() ; ++data_it){
      window.push_back ( *data_it );
      // Do something else
      }

Can anybody tell me where I'm wrong or suggest a way to do this ? BTW I want to push it just element by element because I want to be able to do something else inside the loop too. i.e. I want to check for a condition and increment the value of the iterator inside. for example, if a condition satisfies then I'll do data_it+=3 or something like that inside the loop.

Thanks

P.S. I asked this question last night and didn't get any response and that's why I'm posting it again.

0x0
  • 2,915
  • 5
  • 40
  • 67
  • What's the problem at all? BTW, if you want to push the double values element by element, you need nested for loops. – Flinsch Dec 30 '10 at 16:39
  • What else do you want to do in the loop? Chances are you do not need to copy the item element by element, but we need more information to give you a proper answer. A simple nested for loop would suffice otherwise. – Zac Howland Dec 30 '10 at 16:41
  • Please, could you be clear on your actual question? Your code looks good so far. – Antonio Pérez Dec 30 '10 at 16:45
  • I want to check for a condition and increment the value of the iterator inside. for example, if a condition satisfies then I'll do data_it+=3 or something like that inside the loop. – 0x0 Dec 30 '10 at 16:48
  • 1
    "I asked this question last night and didn't get any response and that's why I'm posting it again." - Please update the original question instead of re-asking the same question. At least very least provide a [link to the original](http://stackoverflow.com/questions/4541556/pushing-an-array-into-a-vector). – moinudin Dec 30 '10 at 16:48
  • I updated it a little bit. That's why I re-posted it. – 0x0 Dec 30 '10 at 16:51
  • You have to tell us what the problem us. Also, please don't just repost when your question doesn't get answered: consider improving the question. :) – Lightness Races in Orbit Jan 03 '11 at 00:14

2 Answers2

0

If what you need is:

to check for a condition and increment the value of the iterator inside. for example, if a condition satisfies then I'll do it+=3 or something like that inside the loop.

Then using a while loop instead of a formigth help:

std::vector< std::vector<double> >::iterator data_it = data.begin();

while (data_it != data.end()) {
  window.push_back ( *data_it );
  if (SatisfiesCondition(*data_it)) {
    data_it += 3;
  }
  else {
    ++data_it;
  }
}
Antonio Pérez
  • 6,702
  • 4
  • 36
  • 61
  • My actual question is window.push_back ( *data_it ); is not working. What would be the alternative ? How can I insert elements into a vector like that ? – 0x0 Dec 30 '10 at 16:58
  • @Antonio: and what if the next-to-last element satisfies the condition ? – icecrime Dec 30 '10 at 17:00
  • @Sunil: How do you know `window.push_back ( *data_it );`is not working? Is it a compiler error or a run time error? – Antonio Pérez Dec 30 '10 at 17:03
  • @icecrime: that would be a problem :). You would have to check that before increment. The `+= 3`is just an example. And I obviiously misunderstood Sunil's question. – Antonio Pérez Dec 30 '10 at 17:05
  • @Antonio: It is a runtime error. It always gives me segmentation fault. – 0x0 Dec 30 '10 at 17:06
  • @Antonio: It is a huge code but I'm having error only in this part. if I don't call this function then everything runs fine. – 0x0 Dec 30 '10 at 17:15
  • 1
    @Sunil: Segmentation fault occurs when accessing memory out of you program's scope. Using a debugger will help. – Antonio Pérez Dec 30 '10 at 17:20
0

Your code currently copies the data row-by-row, not element-by-element.

For element-wise copying of a 2D array, you need a nested loop, as @AmbiguousX already mentioned. If you start with an empty 2D array window, the code should look like this:

for (std::vector< record_t >::iterator i = data.begin(); i != data.end(); i++)
{
  std::vector<double> row;
  for (std::vector<double>::iterator j = i->begin(); j != i->end(); j++)
  {
    row.push_back(*j);
    // do something else with *j
  }
  window.push_back(row);
}
Bart van Ingen Schenau
  • 15,488
  • 4
  • 32
  • 41