Incrementing Iterators

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;}
See also  5 Tips for Working With an Onsite Interpreter

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist