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

前端 未结 13 1448
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  梦毁少年i
    2021-02-05 19:59

    No, it's a bad idea to get a hold on iter.end() before the loop starts. If your loop changes the container then the end iterator may be invalidated. Also, the end() method is guaranteed to be O(1).

    Premature optimization is the root of all evil.

    Also, the compiler may be smarter than you think.

提交回复
热议问题