Does operator precedence in C++ differ for pointers and iterators?
- by oraz
The code below demonstrates this difference:
#include <iostream>
#include <string>
int main()
{
char s[] = "ABCD";
std::string str(s);
char *p = s;
while(*p) {
*p++ = tolower(*p); // <-- incr after assignment
}
std::cout << s << std::endl;
std::string::iterator it = str.begin(), end = str.end();
while(it != end) {
*it++ = tolower(*it); // <-- incr before assignment ?
}
std::cout << str << std::endl;
return 0;
}
the code above outputs:
abcd
bcd
if we separate assignment operation and increment operator:
while(it != end) {
*it = tolower(*it); // <-- incr before assignment ?
it++;
}
the output will be as expected.
What's wrong with the original code?
$ g++ --version
g++ (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
Copyright (C) 2004 Free Software Foundation, Inc.