Sunday, October 17, 2010

[c++] век живи — век учись...

Сколько себя помню, всегда писал в циклах for постинкрементный оператор. А вот оказывается, лучше использовать преинкремент и предекремент:
//make all characters in the list uppercase
list::iterator pos;
for (pos = coll.begin(); pos != coll.end(); ++pos) {
     *pos = toupper(*pos);
}
Note that the preincrement operator (prefix ++) is used here. This is because it might have better performance than the postincrement operator. The latter involves a temporary object because it must return the old position of the iterator. For this reason, it generally is best to prefer ++pos over pos++.
Так-то!