Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assuming that scores is a vector that holds elements of type int, what\'s wrong

ID: 3669544 • Letter: A

Question

Assuming that scores is a vector that holds elements of type int, what's wrong with the following code snippet (meant to increment each element)?
vector<int>::iterator iter; //increment each score for (iter = scores.begin( ); iter != scores.end( ); ++iter) { iter++; } Assuming that scores is a vector that holds elements of type int, what's wrong with the following code snippet (meant to increment each element)?
vector<int>::iterator iter; //increment each score for (iter = scores.begin( ); iter != scores.end( ); ++iter) { iter++; }
vector<int>::iterator iter; //increment each score for (iter = scores.begin( ); iter != scores.end( ); ++iter) { iter++; }

Explanation / Answer

vector<int>::iterator iter;

//increment each score

for (iter = scores.begin( ); iter != scores.end( ); ++iter)

{

iter++;

}

In the above program, iter increments twice.

Here iter variable is incremented twice, post increment and pre increment.

No need to increment the iter variable twice.