in c++ 11 if we have a set<int> S
; we could say:
for (auto i: S)
cout << i << endl;
but can we force i
to be a iterator, I mean write a code that is equivalent to:
for (auto i = S.begin(); i != S.end(); i++)
cout << (i != s.begin()) ? " " : "" << *i;
or could we do something that we can understand the index of i
in the set(or vector)?
and another question is how could we say that don't do this for all elements in S
but for first half of them or all of them except the first one.
or when we have a vector<int> V
, and want to print its first n
values what should we do? I know we can create a new vector but it takes time to copy a vector to a new vector.