Common Iterator for different containers

U

utab

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 << y << z << endl; Bu

So I just wondered if sth is possible with iterators(common ?) in
order to get the container elements

Thx.
 
M

Mike Wahler

utab said:
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 << y << z << 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 << ' ' << vec2 << ' ' << vec3 << '\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
 
A

Aleksander Beluga

utab said:
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 << y << z << 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.
 
B

benben

for (int i = 0; i != 0; ++i)
{
cout << *(iterx + i) << *(itery + i) << *(iterz + i) << endl;
}


Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top