I have an iterator. Say I need to traverse the set not from the beginning but from some particular point. Also it is quite difficult for me to get the values stored in the sets as they are pointers. So how to i modify my code to traverse through my sets from a point that is not the begining. ?
Here is the code:
for(iter=make.at(level).begin();iter!=make.at(level).end();iter++)
{
Function(*iter);
}
Using this gives an error:
for(iter=make.at(level).begin()+10;iter!=make.at(level).end();iter++)
{
Function(*iter);
}
There are different types of iterators:
ForwardIterator,BidirectionalIterator, andRandomAccessIterator.ForwardIteratorallows you to move forward only, using the increment operator.BidirectionalIteratorallows both directions.RandomAccessIteratorallows any advancement, includingoperator+andoperator-.The one you’re thinking in terms of is
RandomAccessIterator, like the one found instd::vector. Whatstd::setuses, though, is theBidirectionalIterator. That means you can only increment and decrement.Therefore, you need to make your iterator outside of your loop and advance it forward ten times. To make it simple,
std::advancedoes this, and has different compatibility forBidirectionalIterator, as well asForwardIterator(linear time because of only one increment at a time), andRandomAccessIterator(constant time due to operator+).