Rob
Rob

Reputation: 181

Vector int iterators

I'm currently trying to run through a vector of ints with an iterator. Every time I run through it, I'm only getting a value of 0. Both vectors do have valid data in them.

OffenseName and Down are private member data that the user inputs.

vector <string> :: iterator itr;
vector <int> :: iterator itrdown;
int count = 0;

for (itr = dataOffenseName.begin(); itr!= dataOffenseName.end(); ++itr)
{
    if ( OffenseName == *itr )
    {
        for (itrdown = dataDown.begin(); itrdown != dataDown.end(); ++itrdown)
        {
            //Here itrdown is always coming at 0. The dataDown vector 
            //does have valid data in it
            if (Down == *itrdown) 
            {
                count++;
            }
            else
            {
                break;
            }
        }
    }
}
return count;   

Upvotes: 0

Views: 116

Answers (2)

L&#225;szl&#243; Papp
L&#225;szl&#243; Papp

Reputation: 53165

if (Down = *itrdown)

should be

if (Down == *itrdown)

The former is not a comparison, just an assignment, meanwhile you meant second: comparison.

However, there is a more important lesson in here: turn the warnings on when building. If you had done, you could not have missed this one unless you disregard warnings without evaluating them which is not a good idea.

That is probably a more important lesson to learn out of this situation because you would not need to debug issues like this at all then. ;-)

Upvotes: 0

Maciej Stachowski
Maciej Stachowski

Reputation: 1728

if (Down = *itrdown) 

Come on, the oldest trick in the book :)

if (Down == *itrdown)

will be correct.

Upvotes: 1

Related Questions