Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Advanced
Jul 12, 2005

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;
Wael Salman
Thanks for your registration, follow us on our social networks to keep up-to-date