Re: User-defined iterator
jddahl@gmail.com writes:
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! ]