470,848 Members | 1,712 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

Common Iterator for different containers

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.

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

Thx.

Mar 1 '06 #1
3 1861

"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

Mar 2 '06 #2
utab wrote:
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.
Maybe you mean if you can use common TYPE of iterator?
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


The analogous code with iterators was already given and it was mentioned
that i in this case is not a pointer but index. As far as I can
remember, yes, iterators support arithmetical operations on them.

And also, check out Boost.Tuple.
Mar 2 '06 #3
for (int i = 0; i != 0; ++i)
{
cout << *(iterx + i) << *(itery + i) << *(iterz + i) << endl;
}
Ben
Mar 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

38 posts views Thread by Grant Edwards | last post: by
26 posts views Thread by Michael Klatt | last post: by
4 posts views Thread by matthurne | last post: by
reply views Thread by toton | last post: by
22 posts views Thread by =?gb2312?B?wNbA1rTzzOzKpg==?= | last post: by
5 posts views Thread by George2 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.