472,992 Members | 3,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

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 1968

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
6
by: greg | last post by:
Hello all, I havent used STL containers much, as I am used to MFC containers/classes always ..The differences that I could see is iterators and algorithms. The algorithms providing some basic...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
4
by: matthurne | last post by:
I am working through exercise 8-2 in Accelerated C++...I am implementing the function equal(b, e, b2) where b is an iterator for the first element in a container, e is an iterator pointing to one...
16
by: forester | last post by:
lets say its common situation when object have subobjects in container and receives callbacks from contained items. and object want to move objects in containers on signal(callback). iterator is...
13
by: Dan Tsafrir | last post by:
is the following code standard? (cleanly compiles under g++-4.0.2): struct Asc { bool operator()(int a, int b) {return a < b;} }; struct Des { bool operator()(int a, int b) {return b > a;} };...
0
by: toton | last post by:
Hi, I have a complex design of some dynamic situation. First I am posting the design, then a few questions regarding the problem I am facing. Hope some of the experts will answer the questions....
22
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
windows xp, visual studio 2005 ---------------------------------------------------------------------------------------------------------------------------------- #include <iostream> #include <map>...
5
by: George2 | last post by:
Hello everyone, Operator * on iterator of type T will result in reference to type T (T&), right (i.e. not type T itself or some other types)? I am looking for some STL implementation code...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.