ki******@yahoo.com wrote:
I have an array or items each item in the array can contain another
array of other items and that array of other items can contain more
array of item. I want recursively iterate through the array and print
out all the items. Can anyone show me some light. Thanks in advance.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Item
{
Item( const char* Name = "" ) : m_Name( Name ) {}
string m_Name;
vector< Item > m_Childs;
};
void Indent( int Level )
{
for( int i = 0; i < Level; ++i )
cout << " ";
}
void PrintIt( int Level, Item& What )
{
Indent( Level );
cout << What.m_Name << endl;
for( size_t i = 0; i < What.m_Childs.size(); ++i )
PrintIt( Level + 2, What.m_Childs[i] );
}
void PrintIt( Item& What )
{
PrintIt( 0, What );
}
int main()
{
Item GrandChild3( "GrandChild3" );
GrandChild3.m_Childs.push_back( Item( "GrandGrandChild1" ) );
Item Child1( "Child1" );
Child1.m_Childs.push_back( Item( "GrandChild1" ) );
Child1.m_Childs.push_back( Item( "GrandChild2" ) );
Child1.m_Childs.push_back( GrandChild3 );
Item Child2( "Child2" );
Child2.m_Childs.push_back( Item( "GrandChild10" ) );
Child2.m_Childs.push_back( Item( "GrandChild11" ) );
Item Root( "Root" );
Root.m_Childs.push_back( Child1 );
Root.m_Childs.push_back( Child2 );
PrintIt( Root );
}
--
Karl Heinz Buchegger
kb******@gascad.at