- 1、for(iterator it = V.begin(); it != V.end(); ++it)
- 2、for(iterator it = V.begin(); it != V.end(); it++)
(1)这两个式子的结果肯定是一样的。
(2)俩式子效率不同。++it返回的是对象引用,而it++返回的是临时的对象。由于it是用户自定义类型的,编译器无法对其进行优化,即无法直接不生成临时对象,进而等价于++it,所以每进行一次循环,编译器就会创建且销毁一个无用的临时对象。
(3)对于int i,i++ 和 ++i 在release下,二者效率是等价的,因为编译器对其进行优化了。
a++返回的是自身的值副本,所以不能作为左值
int int::operator++(int)
{
int oldvalue = *this;
++*this;
return oldvalue;
}
++a 返回的是a加1后自身的引用
int& int::operator ++()
{
*this = *this+1;
return *this;
}