0x0
0x0

Reputation: 3075

Vector's weird behavior?

I've a program that just adds two vectors v1 and v2 like this : v1+=v2. At each iteration v2 gets added to v1. Consider the following program:

    #include <iostream>
    #include <vector>
    #include <iterator>

    using namespace std;

    typedef vector<double> v_t;

    int main(){

    v_t v1;
    v_t v2;

    for (int i = 1; i<10; i++){
      v1.push_back(i);
      v2.push_back(i);
      if (i == 5){            // Just want to insert a 0 inbetween
        v1.push_back(0);
        v2.push_back(0);
      }
    }

 // v1 :  1 2 3 4 5 0 6 7 8 9
 // v2 :  1 2 3 4 5 0 6 7 8 9

    v_t::iterator it2(v2.begin());
      for(v_t::iterator it(v1.begin()), end(v1.end()); it != end; )
      *(it++) += *(it2++);

    copy(v1.begin(), v1.end(), ostream_iterator<double>(cout, " "));
      cout << endl;
    }

The output of the program is:

 2 4 6 8 10 0 12 14 16 18  // This is correct and what I need

but if I modify the for loop like this :

.
.
.
v_t::iterator it2(v2.begin());
  for(v_t::iterator it(v1.begin()), end(v1.end()); it != end && ( *(it++) += *(it2++) ); );

 copy(v1.begin(), v1.end(), ostream_iterator<double>(cout, " "));
      cout << endl;
    }

Now the output is :

2 4 6 8 10 0 6 7 8 9 

i.e. whenever it encounters 0 at the same place in both the vectors it stops adding. Why ? More over 0 doesn't mark the end of any vector, does it? It is also an value.

Please feel free to edit the title of my question if you feel it's not meaningful.

Upvotes: 1

Views: 162

Answers (5)

James
James

Reputation: 5425

That's because you are checking ( *(it++) += *(it2++) ) as a loop termination condition. So when it hits the 6th element, zero, it evaluates to to zero, which fails the condition it != end && ( *(it++) += *(it2++) ); and terminates the loop.

for(v_t::iterator it(v1.begin()), end(v1.end()); it != end && ( *(it++) += *(it2++) ); );

Should be:

for(v_t::iterator it(v1.begin()), end(v1.end()); it != end; ( *(it++) += *(it2++) ) );

Upvotes: 3

finiteautomata
finiteautomata

Reputation: 3813

It's because the result of

( *(it++) += *(it2++) );

is zero when both *it and *it2 are zero. And zero is false in C (so it is in C++).

By the ways, try to separate incrementing the operators and doing the actual work in the vector. It makes the code very messy to do such tricky things together (and it doesn't make you a better programmer ;)

Upvotes: 2

unwind
unwind

Reputation: 400129

If the latter case, your for loop's condition includes the expression *(it++) += *(it2++).

If the both iterators references a 0, this expression will have the value 0, which is false and thus causing the loop to terminate.

Why are you doing the appending as a side-effect in the loop's condition?

Upvotes: 1

Nick Banks
Nick Banks

Reputation: 4408

It is stopping because of the condition part of your loop:

it != end && ( *(it++) += *(it2++) )

When they are both equal to zero, the condition evaluates to false.

Upvotes: 0

sepp2k
sepp2k

Reputation: 370445

Your for-loop's condition is it != end && *(it++) += *(it2++). If both *it and *it2 are 0, *(it++) += *(it2++) will evaluate to 0, so your condition is false and the loop exits.

Rather than putting this in the condition part of your for-statement you should put it in the last part, which you left empty:

for(v_t::iterator it(v1.begin()), end(v1.end()); it != end; *(it++) += *(it2++))

Upvotes: 1

Related Questions