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

前端 未结 13 1473
爱一瞬间的悲伤
爱一瞬间的悲伤 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 19:55

    If you wrap your code into lines properly, the inline form would be equally readable. Besides, you should always do the iterEnd = container.end() as an optimization:

    for (SomeClass::SomeContainer::iterator Iter = m_SomeMemberContainerVar.begin(),
        IterEnd = m_SomeMemberContainerVar.end();
        Iter != IterEnd;
        ++Iter)
    {
    }
    

    Update: fixed the code per paercebal's advice.

提交回复
热议问题