Lets say i have a class Foo. It contains a vector of type Foo. How can write a loop to iterate through the vector in foo and continually iterate through the sub vectors until we reach a level where on of the vectors is empty
class Foo
{
Foo();
std::vector<Foo> foos;
}
I can do this to iterate it through, but how can i iterate through the vectors in the foo objects inside the original vector recursively until i reach a level that the vector is empty?
Foo f;
if( !f->foos.empty() )
{
std::vector<Foo>::const_iterator itr;
for ( itr = f.foos.begin(); itr!=f.foos.end(); ++itr )
{
}
}
Use recursion:
class Foo
{
Foo();
std::vector<Foo> foos;
void iterate()
{
std::vector<Foo>::const_iterator itr;
for ( itr = foos.begin(); itr!=foos.end(); ++itr )
{
// do stuff breadth-first
(*itr).iterate();
// do stuff depth-first
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With