A
ALiX
Hi all,
In my code I use different vectors of the same size to hold some data.
At some point I need to iterate through all vectors at the same time.
Now, the question is what type should the index variable have? size_t?
vector<X>::size_type? vector<Y>::size_type? Example code:
--------------------
vector<X> xv;
vector<Y> yv;
vector<Z> zv;
// Fill xv, yv and zv with elements. All three vectors have the same
size.
for (size_t i = 0; i < xv.size(); ++i) {
// Access/modify xv, yv and zv.
}
--------------------
The above code does not appeal to me because xv.size() returns a
vector<X>::size_type and not size_t. And declaring i with type
vector<X>::size_type is not appealing either, since we would then be
using vector<X>::size_type to index into vector<Y>.
I have heard people say that vector<X>::size_type must be of type
size_t for _all_ types X (even when user-supplied allocators). Is this
true? I can't find such a statement in the standard. Even if true for
vectors I don't think it is true for other containers, like deques or
strings. What if I have a deque and a vector that I want to access with
the same index?
Also, using three iterators in the loop seems ugly.
Regards,
ALiX
In my code I use different vectors of the same size to hold some data.
At some point I need to iterate through all vectors at the same time.
Now, the question is what type should the index variable have? size_t?
vector<X>::size_type? vector<Y>::size_type? Example code:
--------------------
vector<X> xv;
vector<Y> yv;
vector<Z> zv;
// Fill xv, yv and zv with elements. All three vectors have the same
size.
for (size_t i = 0; i < xv.size(); ++i) {
// Access/modify xv, yv and zv.
}
--------------------
The above code does not appeal to me because xv.size() returns a
vector<X>::size_type and not size_t. And declaring i with type
vector<X>::size_type is not appealing either, since we would then be
using vector<X>::size_type to index into vector<Y>.
I have heard people say that vector<X>::size_type must be of type
size_t for _all_ types X (even when user-supplied allocators). Is this
true? I can't find such a statement in the standard. Even if true for
vectors I don't think it is true for other containers, like deques or
strings. What if I have a deque and a vector that I want to access with
the same index?
Also, using three iterators in the loop seems ugly.
Regards,
ALiX