"utab" <um********@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
Hi, there
Assume that I have three vectors x,y and z that has the same number of
elements. Can I use a common iterator for these three.
No. How would the iterator know which container
you want to access?
I did it with iterx, itery and iterz but the thing I wondered was: in a
for loop you can write array elements by using the same index such as;
for (int i = 0 ; i!=10; ++i )
cout << x[i] << y[i] << z[i] << endl; Bu
So I just wondered if sth is possible with iterators(common ?) in
order to get the container elements
No. An iterators is like a pointer, it refers to
a specific object (element of a container). Again,
how could an iterator know which of more than one
container you want to access?
However, note that std::vector type has an index operator,
so you can use the same subscript syntax as with
arrays.
const std::vector<int>::size_type vsize(10);
std::vector<int> vec1(vsize);
std::vector<int> vec2(vsize);
std::vector<int> vec3(vsize);
for(std::vector<int>::size_type i = 0; i != vsize; ++i)
std::cout << vec1[i] << ' ' << vec2[i] << ' ' << vec3[i] << '\n';
Also note that like an array subscript, a vector subscript is not
bounds checked, you must ensure that a subscript is valid. Bounds
checking is available with a vector, by using the 'at()' member
function instead of a subscript.
vec1.at(10); // will cause an exception to be thrown
-Mike