c++使用for循环的最佳方法
我脑子里一直盘旋着这个问题…… 我有一个 std::vector 来迭代: 哪种迭代方式最好(更快)?
这是使用迭代器的代码:
// using the iterator
for( std::vector <myClass*>::iterator it = myObject.begin( ); it != myObject.end( ); it++ )
{
(*it)->someFunction( );
}
这是“正常”模式...
// normal loop
for( int i = 0; i < myObject.Size( ); i++ )
{
myObject[i]->someFunction( );
}
感谢您的建议!
I have this question that runs in my mind...
I have a std::vector to iterate:
which is the best way (the faster) to iterate?
here is the code using an iterator:
// using the iterator
for( std::vector <myClass*>::iterator it = myObject.begin( ); it != myObject.end( ); it++ )
{
(*it)->someFunction( );
}
and here is 'normal' mode...
// normal loop
for( int i = 0; i < myObject.Size( ); i++ )
{
myObject[i]->someFunction( );
}
thanks for your suggestions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
两者实际上都不会更快,因为在大多数实现中,
vector::iterator
只是T*
和size
的 typedef代码> 已缓存。但使用
++it
而不是it++
是一个好习惯。后者涉及创建临时的。在其他具有重要迭代器的容器(例如
map
、list
等)上,后增量和预增量之间的差异可能会变得明显。None of the two will be any faster really, because on most implementations a
vector<T>::iterator
is just a typedef forT*
andsize
is cached.But doing
++it
instead ofit++
is a good habit. The latter involves creating a temporary.On other containers such as
map
,list
etc. with nontrivial iterators the difference between postincrement and preincrement might become noticable.如果您真的关心,您可以发现:只需使用该循环制作一个包含一个函数的单个源文件,然后查看优化后的程序集:
您可以直接看到差异!我敢打赌没有。
也就是说,您应该使用迭代器模式,因为它是理想的、通用的 C++,它能让您处于正确的状态——此外,它适用于比向量更一般的情况!像这样写:
如果您好奇,向量迭代器很可能只是一个本机的原始指针,因此在效率方面确实没有什么可担心的,而且从不言自明的含义中可以享受到很多乐趣,算法风格!
PS 如果你有 C++0x,请这样说:
If you really care, you can find out: Just make a single source file with one function with that loop and look at the optimized assembly:
You can directly see the differences! I bet there are none.
That said, you should use the iterator pattern because it's idomatic, generic C++ and it gets you in the right mood -- plus, it works in far more general cases than just vectors! Write it like this:
In case you're curious, a vector iterator is most likely just going to be a native, raw pointer, so there's really nothing to fear in terms of efficiency, and a lot to be enjoyed from the self-explanatory, algorithmic style!
PS If you have C++0x, say it like this:
第一个代码将分解为递增指针。第二个将增加一个索引,并索引到数组中。第一个可以使用稍小的指令(因此可能更快),假设编译器尚未将第二个指令优化为第一个指令。但这只是微不足道的差异。
然而,迭代器应该是首选,不是因为速度,而是因为您可以轻松移动代码以迭代任何标准 C++ 容器,而不仅仅是
vector
。但是,您还有一些需要改进的地方。
it++
,而是使用++it
。这在 C++ 中非常重要,因为迭代器最终可能会在后增量中做更多的工作,而这些工作不会像int
类型那样被优化。end()
或size()
。对于某些迭代器类型和集合,这可能不会被优化并且可能不是最佳的。vector
的索引时,请使用vector::size_type
。int
不能保证足够大,而size_type
是专门为此设计的。因此,更好的编写方法是:
The first code will decompose into incrementing a pointer. The second one will increment an index, and index into the array. The first one can use slightly smaller instructions (and thus potentially be faster) assuming the compiler doesn't optimize the second into the first already. But it will be a trivial difference.
Iterators should be preferred, however, not because of speed but because you can then easily move the code to iterate any standard C++ container, not just
vector
.However, you've got a few things to improve.
it++
, but++it
. This can be very important in C++ because iterators can end up doing a little more work in post-increment which won't be optimized out as if the type were anint
.end()
orsize()
. For some iterator types and collections this might not be optimized out and can be very sub-optimal.vector::size_type
when you need an index into avector
.int
is not guaranteed to be big enough, whilesize_type
was made specifically for that.So, the better ways to write these are:
这是我使用的:
与其他方法相比,此循环具有以下优点:
它也有一些缺点:
T
根据用途而变化Here's what I use:
This loop has the following advantages over other approaches:
It does have some disadvantages too:
const T &o = vec[i];
or change the data member mutableT
changes depending on the use你可以这样做:
source https://mockstacks.com/Cpp_For_Loop
You can do this:
source https://mockstacks.com/Cpp_For_Loop