soncrash
soncrash

Reputation: 47

Vector iterator and stop condition

I have a problem with stop condition of my infinite loop 'for'. I've declared two iterators for two vector's. Those vectors may contain diffrent number of elements (size of vectors are not equal - mostly).

OK so here's a piece of my code:

vector<int> even;
vector<int> odd;
vector<int>::iterator itEven;
vector<int>::iterator itOdd;

As the name says vector are storing even and odd integers. After filling my vector's with data i want to display content of both vector alternately. I'm doing it here:

for(itEven = even.begin(), itOdd = odd.begin() ;; itEven ++, itOdd ++){
    if(itEven != even.end())
        printf("%d ", *itEven);
    if(itOdd != odd.end())
        printf("%d ", *itOdd);
    if(itEven == even.end() && itOdd == odd.end())
        break;
}

My break instruction works only if vector cointains same number of elements. There's something wrong and I'm getting infinite loop when first vector holding one element and second holding three - for example.

I hope my description of a problem was clear for you, thanks for any respones and help.

Upvotes: 0

Views: 2867

Answers (4)

scohe001
scohe001

Reputation: 15446

If you want to loop all the way through both vectors then you need to stop incrementing when one of them hits the end. Since you'll be conditionally incrementing, put that code in the for body and move the break condition to the for header:

for(itEven = even.begin(), itOdd = odd.begin(); itEven != even.end() || itOdd != odd.end();){
    //Do things...

    //Conditionally increment both iterators
    if(itEven != even.end()) itEven++;
    if(itOdd != odd.end()) itOdd++;
}

Upvotes: 0

makD
makD

Reputation: 11

for(itEven = even.begin(), itOdd = odd.begin() ;; ){
  if(itEven != even.end()){
    printf("%d ", *itEven);
    itEven ++;
  }
  if(itOdd != odd.end()){
    printf("%d ", *itOdd);
    itodd ++;
  }
  if(itEven == even.end() && itOdd == odd.end()){break;}
}

This should work as you want.

Upvotes: 0

quantdev
quantdev

Reputation: 23813

Your code has undefined behavior if both vectors have different sizes :

In your for loop, you increment the iterators even when they went past out the end() :

 itEven ++, itOdd ++ // Once you reach the end of one, you continue to increment its iterator

One of them (the iterator of the smaller vector) will never pass your test:

if(itEven == even.end() && itOdd == odd.end())
        break;

You want to do those increments only until the end() of each, probably within a while() loop, something like :

while(true){
    if(itEven != even.end())
        printf("%d ", *itEven++);
    if(itOdd != odd.end())
        printf("%d ", *itOdd++);
    if(itEven == even.end() && itOdd == odd.end())
        break;
}

Note:

Prefer std::cout to printf

Upvotes: 1

dohashi
dohashi

Reputation: 1841

You are incrementing the iterator of the shorter list past the end of the list. This leads to undefined behaviour. In your case appears it appears the iterator is no longer equal to end.

Upvotes: 0

Related Questions