为什么c ++程序员使用!=而不是& [英] Why do c++ programmers use != instead of <
本文介绍了为什么c ++程序员使用!=而不是&的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
for(vector< int> :: size_type i = 0; i!= 10; ++ i)
for(vector< int> :: size_type i = 0; i< 10; ++ i)
我在加速c ++中读到同样的东西。有人可以解释这个
解决方案
的原理。当使用某些STL迭代器(那些不是随机访问) 必须使用!=
:
(map< int,int> :: iterator i = a.begin(); i!= a.end(); ++ i)...
然而,我没有看到任何理由喜欢!=
如良好的有序标量类型,如在你的例子。对于所有的迭代器类型,我通常喜欢<
和!=
。
In c++ primer, pg 95 the author says that c++ programmers tend to use != in preference of < when writing loops.
for (vector<int>::size_type i = 0; i != 10; ++i) is preferred instead of
for (vector<int>::size_type i = 0; i < 10; ++i)
I read the same thing in accelerated c++. Can someone explain the rationale behind this
解决方案
When using some kinds of STL iterators (those that aren't random access), you must use !=
:
for (map<int,int>::iterator i = a.begin(); i != a.end(); ++i) ...
However, I don't see any reason to prefer !=
for well-ordered scalar types as in your example. I would usually prefer <
for scalar types and !=
for all iterator types.
这篇关于为什么c ++程序员使用!=而不是&的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文