Hello ladies and gents,

Wondered if any of you could help me out here, I'm trying to increment an element of a vector of integers. Here's the code:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	vector<int> scores;
	scores.push_back(500);
	scores.push_back(600);
	scores.push_back(300);

	vector<int>::iterator iter;

	for (iter = scores.begin(); iter != scores.end(); ++iter)
		cout << *iter << endl;

	for (iter = scores.begin(); iter != scores.end(); ++iter)
		???????? // what do I put here

	for (iter = scores.begin(); iter != scores.end(); ++iter)
		cout << *iter << endl;

	cout << "Press enter to exit.\n";;

	cin.ignore(cin.rdbuf()->in_avail() + 1);

	return 0;
}

My idea is to increment the scores by 1 so that the values are:
501
601
301

How can I do that?

Any help would be appreciated :D

Recommended Answers

All 6 Replies

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

Parens are there because a lot of people have trouble with the associativity of operators that have equal precedence. It's right to left in this case, so you can safely remove the parens if you want and the dereference is still performed first.

Parens are there because a lot of people have trouble with the associativity of operators that have equal precedence. It's right to left in this case, so you can safely remove the parens if you want and the dereference is still performed first.

Thanks Narue,

One question if you don't mind, I tried it with and without parenthesis (<-- spelling?) and like you said it worked perfectly.

But, when I tried to do this postfix without parenthesis, the program just stayed in an endless loop, but not when using parenthesis.

Have you any idea why it is important to use parenthesis when using postfix?

Maybe I know the answer because you allready gave it --> because of the dereferencing (associativity of operators)?

>Have you any idea why it is important to use parenthesis when using postfix?
Postfix has a higher precedence than prefix. Prefix has the same precedence as indirection. So *iter++ will increment iter first, then dereference the result of the postfix increment. So the effect is that you're walking through the vector, dereferencing the iterators along the way, but doing nothing with the values. Care to guess what kind of havoc you create by incrementing the iterator twice while only testing it once? :)

Unless you're well versed with the precedence and associativity of the operators, you're better off using parens to avoid any subtle problems. In fact, since you won't always be the only one reading and working with your code, it's really better to use parens even after you're familiar with the precedence tables. You write for the compiler, and yourself, and everyone else who's unlucky enough to read your code. ;)

...and everyone else who's unlucky enough to read your code. ;)

LOL :lol:

Thanks for the explanation ;)

And a Happy New Year :!:

Tried combining those for loops?

for (iter = scores.begin(); iter != scores.end(); iter++)
{
    cout << *iter << " --> ";
    ++(*iter);
    cout << *iter << endl;
}

Tried combining those for loops?

No, I didn't, but then again, it wasn't really needed since it was just an example, but thanks for mentioning it ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.