Iterators

W

wombat

Is there a way to store the addresses of containers in a container and
have an iterator point to each of the containers in this container?

Example:
vector<string> container_a, container_b, container_ix;

container_a and container_b would be stored in container_ix and an
iterator pointing to a position in container_ix to determine whether you
are looking at container_a or container_b.
 
A

Alf P. Steinbach

* wombat:
Is there a way to store the addresses of containers in a container and
have an iterator point to each of the containers in this container?

Example:
vector<string> container_a, container_b, container_ix;

container_a and container_b would be stored in container_ix

typedef StringVector vector<string>;
vector<StringVector> ix(2);
StringVector& a = ix[0];
StringVector& b = ix[1];

and an
iterator pointing to a position in container_ix to determine whether you
are looking at container_a or container_b.

Huh.

What is the problem you're trying to solve?
 
W

wombat

"Alf P. Steinbach said:
* wombat:
Is there a way to store the addresses of containers in a container and
have an iterator point to each of the containers in this container?

Example:
vector<string> container_a, container_b, container_ix;

container_a and container_b would be stored in container_ix

typedef StringVector vector<string>;
vector<StringVector> ix(2);
StringVector& a = ix[0];
StringVector& b = ix[1];

and an
iterator pointing to a position in container_ix to determine whether you
are looking at container_a or container_b.

Huh.

What is the problem you're trying to solve?

Ok, container_ix contains container_a and container_b:

container_ix.push_back(&container_a);
container_ix.push_back(&container_b);

itr=container_ix.begin();
while(itr!=container_ix.end())
{
// With the first pass, itr points to container_a.
// With the second pass, itr points to container_b.
itr++;
}

Of course this code doesn't work, but it gives (hopefully) an idea of
what I'm looking to try to do.
 
A

Alf P. Steinbach

* wombat:
Alf P. Steinbach said:
* wombat:
Is there a way to store the addresses of containers in a container and
have an iterator point to each of the containers in this container?

Example:
vector<string> container_a, container_b, container_ix;

container_a and container_b would be stored in container_ix
typedef StringVector vector<string>;
vector<StringVector> ix(2);
StringVector& a = ix[0];
StringVector& b = ix[1];

and an
iterator pointing to a position in container_ix to determine whether you
are looking at container_a or container_b.
Huh.

What is the problem you're trying to solve?

Ok, container_ix contains container_a and container_b:

container_ix.push_back(&container_a);
container_ix.push_back(&container_b);

itr=container_ix.begin();
while(itr!=container_ix.end())
{
// With the first pass, itr points to container_a.
// With the second pass, itr points to container_b.
itr++;
}

Of course this code doesn't work, but it gives (hopefully) an idea of
what I'm looking to try to do.

Huh, the code should work just fine. Provided, of course, you declare
container_ix correctly. Assuming

vector<string> container_a, container_b;

that would be

vector< vector<string>* > container_ix;

By the way, please look up the FAQ's posting guidelines, about how to
post a question about Code That Does Not Work.

Hth.,

- Alf
 
W

wombat

"Alf P. Steinbach said:
* wombat:
Alf P. Steinbach said:
* wombat:
Is there a way to store the addresses of containers in a container and
have an iterator point to each of the containers in this container?

Example:
vector<string> container_a, container_b, container_ix;

container_a and container_b would be stored in container_ix
typedef StringVector vector<string>;
vector<StringVector> ix(2);
StringVector& a = ix[0];
StringVector& b = ix[1];


and an
iterator pointing to a position in container_ix to determine whether you
are looking at container_a or container_b.
Huh.

What is the problem you're trying to solve?

Ok, container_ix contains container_a and container_b:

container_ix.push_back(&container_a);
container_ix.push_back(&container_b);

itr=container_ix.begin();
while(itr!=container_ix.end())
{
// With the first pass, itr points to container_a.
// With the second pass, itr points to container_b.
itr++;
}

Of course this code doesn't work, but it gives (hopefully) an idea of
what I'm looking to try to do.

Huh, the code should work just fine. Provided, of course, you declare
container_ix correctly. Assuming

vector<string> container_a, container_b;

that would be

vector< vector<string>* > container_ix;

By the way, please look up the FAQ's posting guidelines, about how to
post a question about Code That Does Not Work.

Hth.,

- Alf

Ok, I glanced over the FAQ about posting.. didn't see anything. There
were several links that I visited and still didn't find it. I even did a
search. Came up with two results but off hand still didn't find
anything. I didn't post how I declared the container, didn't think of
that so if that was the problem sorry for any problems I'm causing.

I got the code to work, declared the iterator as follows:

vector<vector<string>*>::iterator itr;

It does hold the correct address of the container it's pointing to (like
a pointer should) but actually using it is still a problem. Using *itr
would work for a regular iterator.

I've googled for anything I could think of that would relate to what I'm
trying to do but it's like trying to find a needle in a hay stack, and I
haven't even found the needle yet - or if I'm looking up info correctly.
 
V

Victor Bazarov

wombat said:
[..]
By the way, please look up the FAQ's posting guidelines, about how to
post a question about Code That Does Not Work.

Hth.,

- Alf

Ok, I glanced over the FAQ about posting.. didn't see anything.

Didn't see section 5? Then please do go back and this time _read_ it,
don't just "glance over".

V
 
L

Lionel B

Alf P. Steinbach said:
* wombat:
* wombat:
Is there a way to store the addresses of containers in a container
and have an iterator point to each of the containers in this
container?

Example:
vector<string> container_a, container_b, container_ix;

container_a and container_b would be stored in container_ix
typedef StringVector vector<string>;
vector<StringVector> ix(2);
StringVector& a = ix[0];
StringVector& b = ix[1];


and an
iterator pointing to a position in container_ix to determine
whether you are looking at container_a or container_b.
Huh.

What is the problem you're trying to solve?

Ok, container_ix contains container_a and container_b:

container_ix.push_back(&container_a);
container_ix.push_back(&container_b);

itr=container_ix.begin();
while(itr!=container_ix.end())
{
// With the first pass, itr points to container_a. // With the second
pass, itr points to container_b. itr++;
}

Of course this code doesn't work, but it gives (hopefully) an idea of
what I'm looking to try to do.

Huh, the code should work just fine. Provided, of course, you declare
container_ix correctly. Assuming

vector<string> container_a, container_b;

that would be

vector< vector<string>* > container_ix;

By the way, please look up the FAQ's posting guidelines, about how to
post a question about Code That Does Not Work.

Ok, I glanced over the FAQ about posting.. didn't see anything.

Post compileable, complete, minimal code + error messages:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Alf P. Steinbach said:
* wombat:
* wombat:
Is there a way to store the addresses of containers in a container and
have an iterator point to each of the containers in this container?

Example:
vector<string> container_a, container_b, container_ix;

container_a and container_b would be stored in container_ix
typedef StringVector vector<string>;
vector<StringVector> ix(2);
StringVector& a = ix[0];
StringVector& b = ix[1];


and an
iterator pointing to a position in container_ix to determine whether you
are looking at container_a or container_b.
Huh.

What is the problem you're trying to solve?

Ok, container_ix contains container_a and container_b:

container_ix.push_back(&container_a);
container_ix.push_back(&container_b);

itr=container_ix.begin();
while(itr!=container_ix.end())
{
// With the first pass, itr points to container_a.
// With the second pass, itr points to container_b.
itr++;
}

Of course this code doesn't work, but it gives (hopefully) an idea of
what I'm looking to try to do.

Huh, the code should work just fine. Provided, of course, you declare
container_ix correctly. Assuming

vector<string> container_a, container_b;

that would be

vector< vector<string>* > container_ix;

By the way, please look up the FAQ's posting guidelines, about how to
post a question about Code That Does Not Work.

Hth.,

- Alf

Ok, I glanced over the FAQ about posting.. didn't see anything. There
were several links that I visited and still didn't find it. I even did a
search. Came up with two results but off hand still didn't find
anything. I didn't post how I declared the container, didn't think of
that so if that was the problem sorry for any problems I'm causing.

I got the code to work, declared the iterator as follows:

vector<vector<string>*>::iterator itr;

It does hold the correct address of the container it's pointing to (like
a pointer should) but actually using it is still a problem. Using *itr
would work for a regular iterator.

Just a guess (since I can't really understand your problem either) but
is your problem the fact that when you dereference iter such as this:

*iter

You can't use what is returned? In that case the solution is simple,
what *iter returns is a std::vector<string>* (a pointer to a vector of
strings, so you have to dereference that pointer too: *(*iter), or when
calling one of the vector's methods: (*iter)->begin().
 
W

wombat

Lionel B <[email protected]> said:
Alf P. Steinbach said:
* wombat:
* wombat:
Is there a way to store the addresses of containers in a container
and have an iterator point to each of the containers in this
container?

Example:
vector<string> container_a, container_b, container_ix;

container_a and container_b would be stored in container_ix
typedef StringVector vector<string>;
vector<StringVector> ix(2);
StringVector& a = ix[0];
StringVector& b = ix[1];


and an
iterator pointing to a position in container_ix to determine
whether you are looking at container_a or container_b.
Huh.

What is the problem you're trying to solve?

Ok, container_ix contains container_a and container_b:

container_ix.push_back(&container_a);
container_ix.push_back(&container_b);

itr=container_ix.begin();
while(itr!=container_ix.end())
{
// With the first pass, itr points to container_a. // With the second
pass, itr points to container_b. itr++;
}

Of course this code doesn't work, but it gives (hopefully) an idea of
what I'm looking to try to do.

Huh, the code should work just fine. Provided, of course, you declare
container_ix correctly. Assuming

vector<string> container_a, container_b;

that would be

vector< vector<string>* > container_ix;

By the way, please look up the FAQ's posting guidelines, about how to
post a question about Code That Does Not Work.

Ok, I glanced over the FAQ about posting.. didn't see anything.

Post compileable, complete, minimal code + error messages:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

I found the answer to my problem by accident, about an hour ago.
Everything is working as it should now. Thanks.
 

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,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top