I have std :: set, and I wanted to iterate through pairs of elements in the set, so I wrote 2 for such loops:
for(std::set<T>::iterator i=mySet.begin();i!=mySet.end();++i)
{
for(std::set<T>::iterator j=i+1;j!=mySet.end();++j)
{
// do something
}
}
The compiler told me that I cannot add numbers to the iterator. However, I can increase and decrease them. Workaround I find that I can skip the first iteration:
for(std::set<T>::iterator i=mySet.begin();i!=mySet.end();++i)
{
std::set<T>::iterator j=i;
for(++j;j!=mySet.end();++j)
{
// do something
}
}
Why can't I just add a number, why do I need to increment?
source
share