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; }
he concludes:
abcd bcd
if we separate the assignment operation and the increment operator:
while(it != end) { *it = tolower(*it);
the conclusion will be as expected.
What is wrong with the source code?
$ g++ --version g++ (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125) Copyright (C) 2004 Free Software Foundation, Inc.
c ++ stl
Oleg Razgulyaev
source share