Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Advanced
Jul 11, 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
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap