C++ STL: How to iterate vector while requiring access to element and its index?
I frequently find myself requiring to iterate over STL vectors. While I am doing this I require access to both the vector element and its index.
I used to do this as:
typedef std::vector<Foo> FooVec;
typedef FooVec::iterator FooVecIter;
FooVec fooVec;
int index = 0;
for (FooVecIter i = fooVec.begin(); i != fooVec.end(); ++i, ++index)
{
Foo& foo = *i;
if (foo.somethingIsTrue()) // True for most elements
std::cout << index << ": " << foo << std::endl;
}
After discovering BOOST_FOREACH, I shortened this to:
typedef std::vector<Foo> FooVec;
FooVec fooVec;
int index = -1;
BOOST_FOREACH( Foo& foo, fooVec )
{
++index;
if (foo.somethingIsTrue()) // True for most elements
std::cout << index << ": "开发者_运维百科 << foo << std::endl;
}
Is there a better or more elegant way to iterate over STL vectors when both reference to the vector element and its index is required?
I am aware of the alternative: for (int i = 0; i < fooVec.size(); ++i)
But I keep reading about how it is not a good practice to iterate over STL containers like this.
for (size_t i = 0; i < vec.size(); i++)
elem = vec[i];
Vectors are a thin wrapper over C arrays; whether you use iterators or indexes, it's just as fast. Other data structures are not so forgiving though, for example std::list.
You can always compute the index in the loop:
std::size_t index = std::distance(fooVec.begin(), i);
For a vector, this is quite likely to be implemented as a single pointer subtraction operation, so it's not particularly expensive.
Elegance is in the eye of the beholder, however do remember pointer/iterator arithmetics :)
for (FooVecIter i = fooVec.begin(); i != fooVec.end(); ++i)
{
Foo& foo = *i;
if (foo.somethingIsTrue()) // True for most elements
std::cout << i - fooVec.begin() << ": " << foo << std::endl;
}
The up-side compared to the distance method is that you won't mistakenly do this for a non-random_access_iterator, so you'll always be in O(1).
For the specific question:
Is there a better or more elegant way to iterate over STL vectors when both reference to the vector element and its index is required?
IMHO,
for (size_t i = 0; i < fooVec.size(); ++i) {
Foo & foo = fooVec[i]; // if 'foo' is to be modified
Foo const& foo = fooVec[i]; // if 'foo' is to be NOT modified
}
is the most simple and elegant solution. Per the requirements of the question, using an iterator is not required.
精彩评论