template<class It>
struct indexer {
It it;
It operator*()const{return it;}
void operator++(){++it;}
void operator++(int){++it;}
bool operator==(indexer const& o)const{ return it == o.it; }
bool operator!=(indexer const& o)const{ return it != o.it; }
};
template<class It>
struct range {
It b,e;
It begin()const{return b;}
It end()const{return e;}
};
range<indexer<std::size_t>> counting( std::size_t begin, std::size_t end ) {
std::cout << begin << "->" << end << "\n";
return {{begin}, {end}};
}
template<class X>
auto indexes_of( X const& x ) {
using std::size;
return counting(0, size(x));
}
now I can make your code do what you thought it did:
vector<int> num{ 1,2,3,4,5 };
for (auto i : indexes_of(num))
{
cout << num[i] << endl;
}
Live example.
Your problem was mistaking indexes into the vector for the elements. Range-for returns the elements. indexes_of
makes it return the indexes for the container in question.