C ++中的前后递增/递减运算符在循环中具有相同的性能吗? [英] Does Pre and post increment/decrement operators in C++ have same performance in a loop?

查看:202
本文介绍了C ++中的前后递增/递减运算符在循环中具有相同的性能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下两个例子。

class ClassOne
{
 //class definition is here
};

std::vector< ClassOne > myListOfObjects;

std::vector< ClassOne >::const_iterator iter = myListOfObjects.begin();

Example 1: 
for( ; iter < myListOfObjects.end(); **++iter**)
{
   //some operations
}

OR

Example 2: 
for( ; iter < myListOfObjects.end(); **iter++**)
{
   //some operations
}

哪一个更快? ++ iter或iter ++在循环的上下文中。

Which one is faster ? ++iter or iter++ in the context of loop.

从Brian的帖子

您也可以尝试以下类似问题之一:此处此处此处此处

You could also try one of these similar questions: here or here or here or here.

推荐答案

this answer ,pre更快。它们只有在处理原语(int,char等)时才是相同的。在这种情况下,他们调用重载运算符。

As noted in this answer, pre is faster. They are only the same when you are dealing with primitives (int, char, etc.). In this case, they are calling overloaded operators.

这篇关于C ++中的前后递增/递减运算符在循环中具有相同的性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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