Consider code snippet 1
:
#include <set>
#include <cstdio>
int main() {
std::set<int> intset = {1, 2, 3, 4, 5, 6};
for(std::set<int>::iterator it1 = intset.begin(); it1 != intset.end(); it1++)
for(std::set<int>::iterator it2 = it1 + 1; it2 != intset.end(); it2++)
printf("Pair {%d,%d}\n", *it1, *it2);
}
This does not compile. See here.
Yet, I am not able to see how this way of iterating over all pairs is any different from the accepted answer to the question here.
Paraphrasing from the accepted answer there, consider code snippet 2
:
for (auto it1 = intset.begin(); it1 != intset.end(); ++it1) {
for (auto it2 = it1; ++it2 != intset.end(); /**/) {
printf("Pair {%d,%d}\n", *it1, *it2);
}
}
I have verified that this above code indeed works fine. Yet, what is the underlying reason that the code snippet 1
does not compile, while code snippet 2
above does?
In both cases, are we not just incrementing a set iterator? In one case, we are doing it2 = it1 + 1
, while in the other, we are doing ++it2
.
it1 + 1
is a random access operation, so it requires the iterator to be a random access iterator, and since std::set<int>::iterator
is not a random access iterator, it does not support this operation.
++it2
requires that the iterator is a forward iterator, and since std::set<int>::iterator
is a bidirectional iterator, it is a forward iterator.