I have a vector:
std::vector<int> vec = {1, 2, 3};
And I want to make a reverse for
loop. It works, when I write:
for(int i = vec.size() - 1; i >= 0; --i) {
std::cout << i << std::endl; // 2, 1, 0
}
But I get a very large number (like 18446744073709223794) if I write:
for(size_t i = vec.size() - 1; i >= 0; --i) {
std::cout << i << std::endl;
}
But they both work when I write:
for(int i = 0; i < vec.size() - 1; ++i) {
std::cout << i << std::endl; // 1, 2, 3
}
// Or
for(size_t i = 0; i < vec.size() - 1; ++i) {
std::cout << i << std::endl; // 1, 2, 3
}
Why do I get the wrong size of the vector when I use size_t
?
I think there is a problem with the conversion.
vec.rbegin()
andvec.rend()
to iterate backwards. – Hyperesthesiasize_t
– Fled