I came across the following strange looking code snippet online (simplified).
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> myset{1, 2, 3, 4, 5};
set<int, int>::iterator it = myset.begin();
cout << *it << endl;
return 0;
}
I just verified this compiles and runs. (it prints 1 when executed)
I don't understand the iterator type definition.
Defining myset as set<int, int> myset; is invalid as the second optional template parameter of the set type declaration must be a comparator supporting bool operator()(int& const lhs, int& const rhs) const.
But why does the iterator definition not fail? Shouldn't it's underlying container have the same type as the target container ?