 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Fri Sep 15, 2006 1:45 am Post subject: User-defined iterator |
|
|
Object A is composed of a vector of A (itself) and a vector of B. I'm
running into difficulties creating an iterator of all the Bs (those
contained within the vector of As as well as the vector of Bs.)
Somehow I have to maintain the state within the iterator of which A and
which B of that A I'm on, but this seems to break the recursive nature
of "A is composed of As."
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Jens Theisen Guest
|
Posted: Fri Sep 15, 2006 4:58 am Post subject: Re: User-defined iterator |
|
|
jddahl (AT) gmail (DOT) com writes:
| Quote: | Object A is composed of a vector of A (itself) and a vector of B. I'm
running into difficulties creating an iterator of all the Bs (those
contained within the vector of As as well as the vector of Bs.)
Somehow I have to maintain the state within the iterator of which A and
which B of that A I'm on, but this seems to break the recursive nature
of "A is composed of As."
|
So you want to do a recursive traversal through this structure as in
struct B { };
struct A
{
std::vector< A > m_a;
std::vector< B > m_b;
};
void traverse(A const& a)
{
for(size_t i = 0; i < m_a.size(); ++i)
{
traverse(a.m_a[i]);
}
for(size_t i = 0; i < m_b.size(); ++i)
{
do_stuff(a.m_b[i]);
}
}
Is that correct?
Can you be a bit more precise on the problem?
If you want to make a proper iterators that's a bit more difficult and
you probably want to avoid that if you just want to get a particular
problem sovled.
Also note that the definition of A is invalid since A is incomplete in
the instantiation of vector< A >, but that's not a problem in
practice - no STL will actually complain.
Best regards,
Jens
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Fri Sep 15, 2006 4:59 am Post subject: Re: User-defined iterator |
|
|
jddahl (AT) gmail (DOT) com wrote:
| Quote: | Object A is composed of a vector of A (itself) and a vector of B. I'm
running into difficulties creating an iterator of all the Bs
|
You should be running into difficulties creating the above. STL
containers require complete types, even the requirement does not
always translate into a "material limitation".
In other words, maybe, depending on how loose the compiler is,
you might get away with creating a class A that has a data member
vector<A>, since the *physical contents* (the data) of the
vector<A> is, at most, pointers to A, and so no infinite
recursion is created.
But the thing is, vector *requires* a complete type, and a
compliant compiler has the right to (and should?) enforce
such requirement.
Am I misunderstanding something?
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Thomas Tutone Guest
|
Posted: Fri Sep 15, 2006 4:59 am Post subject: Re: User-defined iterator |
|
|
jddahl (AT) gmail (DOT) com wrote:
| Quote: | Object A is composed of a vector of A (itself) and a vector of B.
|
Can you give an example? At first glance, this sounds impossible.
What you are describing is something like this:
struct A {
std::vector<A> vec_a;
std::vector<B> vec_b;
};
That's not possible, because A is an incomplete type for purposes of
the vector declaration, which should cause a compile error. Perhaps
you meant Object A is composed of a vector of (preferably smart)
pointers to A and a vector of Bs?
| Quote: | I'm
running into difficulties creating an iterator of all the Bs (those
contained within the vector of As as well as the vector of Bs.)
Somehow I have to maintain the state within the iterator of which A and
which B of that A I'm on, but this seems to break the recursive nature
of "A is composed of As."
|
A recursive algorithm can we rewritten iteratively, although it may be
substantially more complex. Presumably the iterator could push a
pointer to each A it encountered onto a stack or other suitable
container, to ensure you traversed every A of the object. But your
iterator will not be lightweight if it has to maintain such a
structure.
Best regards,
Tom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|