Yes, but I'd use vec.data()
instead. A bonus of using .data()
is that non-contiguous std
containers don't have it, so your code reliably stops compiling when the container being iterated over doesn't work that way (like deque
or std::vector<bool>
). (There are other minor advantages, like std::addressof
issues, and the fact it is well defined on empty containers, but those aren't as important especially here.)
Alternatively we write an index_t
iterator-like wrapper:
template<class T>
struct index_t {
T t;
T operator*()const{ return t; }
void operator++() { ++t; }
friend bool operator==( index_t const& lhs, index_t const& rhs ) {
return lhs.t == rhs.t;
}
friend bool operator!=( index_t const& lhs, index_t const& rhs ) {
return lhs.t != rhs.t;
}
};
template<class T>
index_t<T> index(T t) { return {t}; }
index_t<int>
can be used to create counting for(:)
loops.
index_t<iterator>
can be used to create iterator-returning for(:)
loops.
template<class It>
struct range_t {
It b,e;
It begin() const {return b;}
It end() const {return e;}
};
template<class It>
range_t<It> range( It s, It f ) { return {s,f}; }
template<class T>
range_t<index_t<T>>
index_over( T s, T f ) {
return {{{s}}, {{f}}};
}
template<class Container>
auto iterators_of( Container& c ) {
using std::begin; using std::end;
return index_over( begin(c), end(c) );
}
we can now iterator over iterators of a container.
for (auto it : iterators_of(vec))
live example.
The mentioned iterate-over-integers is:
for (int i : index_over( 0, 100 ) )
we can also directly get the indexes of the container:
template<class Container>
range_t< index_t<std::size_t> >
indexes_of( Container& c ) {
return index_over( std::size_t(0), c.size() );
}
template<class T, std::size_t N>
range_t< index_t<std::size_t> >
indexes_of( T(&)[N] ) {
return index_over( std::size_t(0), N );
}
which lets us:
for( auto i : indexes_of( vec ) )
where i
varies from 0
to vec.size()-1
. I find this is easier to work with sometimes than a zip iterator or the like.
Improvements omitted:
Make index_t
a real input_iterator
. Use std::move
and/or std::forward
as needed in making indexes and ranges. Support Sentinals on ranges. Make range_t
interface richer (size
, optional random-access []
, empty
, front
, back
, range_t range_t::without_front(n) const
, etc.
auto
anyway you better useauto
forindex
rather thanint
– Bridgettebridgewatervec
(other than with the iterator-based version). You should usefor(std::string const& str : vec)
orfor(auto const& str : vec)
for read-only access instead. – Discourseiter::enumerate
– Ethelstan