Incrementing Iterators

You can use iterators for a STL set object to loop through the set elements 1 by 1. But did you know you can also increment the iterator by more than 1? Here’s an example:

const int GetNextProduct(const unsigned int uiIndex){    ProductSet::const_iterator        itElement, it = m_ProductSet.begin();    unsigned int                    ui = 0;    while ((++ui - 1) <= uiIndex)    {        itElement = m_ProductSet.find(*it);        if (itElement == m_ProductSet.end())        {            TraceW("No more products found, found [%u] products in total", ui - 1);            return -1;        }        ++it; // Can I also do it += 2???    }    return *itElement;}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Related Posts