principlecppCritical
Iteration over std::vector: unsigned vs signed index variable
Viewed 0 times
indexsignediterationstdovervectorvariableunsigned
Problem
What is the correct way of iterating over a vector in C++?
Consider these two code fragments, this one works fine:
and this one:
which generates
The
Consider these two code fragments, this one works fine:
for (unsigned i=0; i < polygon.size(); i++) {
sum += polygon[i];
}and this one:
for (int i=0; i < polygon.size(); i++) {
sum += polygon[i];
}which generates
warning: comparison between signed and unsigned integer expressions.The
unsigned variable looks a bit frightening to me and I know unsigned variables can be dangerous if not used correctly, so - is this correct?Solution
For iterating backwards see this answer.
Iterating forwards is almost identical. Just change the iterators / swap decrement by increment. You should prefer iterators. Some people tell you to use
Using std::vector
Using iterators
Important is, always use the prefix increment form for iterators whose definitions you don't know. That will ensure your code runs as generic as possible.
Using Range C++11
Using indices
Using arrays
Using iterators
Using Range C++11
Using indices
Read in the backward iterating answer what problem the
Iterating forwards is almost identical. Just change the iterators / swap decrement by increment. You should prefer iterators. Some people tell you to use
std::size_t as the index variable type. However, that is not portable. Always use the size_type typedef of the container (While you could get away with only a conversion in the forward iterating case, it could actually go wrong all the way in the backward iterating case when using std::size_t, in case std::size_t is wider than what is the typedef of size_type):Using std::vector
Using iterators
for(std::vector::iterator it = v.begin(); it != v.end(); ++it) {
/* std::cout << *it; ... */
}Important is, always use the prefix increment form for iterators whose definitions you don't know. That will ensure your code runs as generic as possible.
Using Range C++11
for(auto const& value: a) {
/* std::cout << value; ... */Using indices
for(std::vector::size_type i = 0; i != v.size(); i++) {
/* std::cout << v[i]; ... */
}Using arrays
Using iterators
for(element_type* it = a; it != (a + (sizeof a / sizeof *a)); it++) {
/* std::cout << *it; ... */
}Using Range C++11
for(auto const& value: a) {
/* std::cout << value; ... */Using indices
for(std::size_t i = 0; i != (sizeof a / sizeof *a); i++) {
/* std::cout << a[i]; ... */
}Read in the backward iterating answer what problem the
sizeof approach can yield to, though.Code Snippets
for(std::vector<T>::iterator it = v.begin(); it != v.end(); ++it) {
/* std::cout << *it; ... */
}for(auto const& value: a) {
/* std::cout << value; ... */for(std::vector<int>::size_type i = 0; i != v.size(); i++) {
/* std::cout << v[i]; ... */
}for(element_type* it = a; it != (a + (sizeof a / sizeof *a)); it++) {
/* std::cout << *it; ... */
}for(auto const& value: a) {
/* std::cout << value; ... */Context
Stack Overflow Q#409348, score: 857
Revisions (0)
No revisions yet.