STL的前和后增量运算符之间的概念差异 [英] Concept difference between pre and post increment operator for STL

查看:83
本文介绍了STL的前和后增量运算符之间的概念差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设为:

for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
{}

对于 int 等内置类型的前/后增量,但是在迭代器方面, ++ iter iter ++ ? (请记住,我知道两者在这里都产生相同的结果)。

I do understand the difference when it comes to pre/post increment for built-in types like int etc but in terms of an iterator, what's the difference here between ++iter and iter++? (Bear in mind that I do know that both yield the same result here).

推荐答案

区别是他们不会相同的结果,而这个特定的例子将做同样的,不管使用的增量形式。预递增形式首先递增该值,然后返回它;而后递增形式递增结果,但返回递增之前的值。这对于基本类型通常是无成本的,但是对于像迭代器这样的东西,它需要创建一个临时值来保存未递增的值,以便以后返回。

The difference is they do not yield the same result, while this particular example will do the same regardless of the increment form used. The pre-increment form first increments the value, and then returns it; while the post-increment form increments the result but returns the value previous to incrementation. This is usually a no cost for fundamental types, but for things like iterators it requires creating a temporary value to hold the non-incremented value, in order to be later returned.

这篇关于STL的前和后增量运算符之间的概念差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆