i += 1; i++; ++i的区别

来源:互联网 发布:淘宝达人申请直播条件 编辑:程序博客网 时间:2024/05/07 10:05

i += 1;

_Myt& operator+=(difference_type _Off)

{ // increment by integer
*(_Mybase *)this += _Off;
return (*this);
}


++i;

_Myt& operator++()
{ // preincrement
++*(_Mybase *)this;
return (*this);
}

i++;
_Myt operator++(int)
{ // postincrement
_Myt _Tmp = *this;
++*this;
return (_Tmp);
}

从以上三者的具体实现可以看出i++返回i而不是i + 1的结果,并且i++过程中会有局部变量的申明初始化和释放,i += 1;和++i,并没有;所以在for循环中,++i的效率比i++更高。


原创粉丝点击