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.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.