Is Iterator initialization inside for loop considered bad style, and why?

前端 未结 13 1444
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 19:37

Typically you will find STL code like this:

for (SomeClass::SomeContainer::iterator Iter = m_SomeMemberContainerVar.begin(); Iter != m_SomeMemberContainerVar.end         


        
13条回答
  •  不思量自难忘°
    2021-02-05 20:09

    I agree with Ferruccio. The first style might be preferred by some in order to pull the end() call out of the loop.

    I might also add that C++0x will actually make both versions much cleaner:

    for (auto iter = container.begin(); iter != container.end(); ++iter)
    {
       ...
    }
    
    auto iter = container.begin();
    auto endIter = container.end();
    for (; iter != endIter; ++iter)
    {
       ...
    }
    

提交回复
热议问题