V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
lixiang
V2EX  ›  问与答

对于C++中的循环的效率问题的疑问

  •  
  •   lixiang · Apr 27, 2012 · 3001 views
    This topic created in 5122 days ago, the information mentioned may be changed or developed.
    对于C++中的循环,比如一个vector<int> a;
    for(vector<int>::iterator it = a.begin(); it != a.end(); ++it)和把 it != a.end()从循环拿出,变成 end = a.end();
    再for(vector<int>::iterator it = a.begin(); it != end; ++it),效率上有很大区别么,同样的问题,比如循环中用到size()函数
    1 replies    1970-01-01 08:00:00 +08:00
    keakon
        1
    keakon  
       Apr 30, 2012
    如果你在遍历时插入或删除了vector中的元素,end()是可能变化的。不过vector::end()会创建iterator对象,开销有点大。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   944 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:07 · PVG 04:07 · LAX 13:07 · JFK 16:07
    ♥ Do have faith in what you're doing.