c++ - Iterating over a vector defined in a struct -


i working on non-binary tree structure, , have struct defined follows has data , it's child in vector called child.

struct node{    string data;    vector< node* > child;    vector<node*>::iterator    int count; }; 

i have function have defined print child in vectors, can't iterator working

void printtree(node* &a){      for(a->i = a->child.begin(); a->i !=a->child.end();++i)         cout << *(a->i)->data <<endl; } 

i getting error iterator isn't defined when printtree called. tried defining iterator inside printtree function keep getting error. suggestions on how should change code?

please take iterator out of node structure.

void printtree(node* &a) {     for( vector<node*>::iterator = a->child.begin(); != a->child.end(); i++ )     {         cout << (*i)->data << endl;     } } 

[edit]

even confused myself when wrote answer. quite save myself ugly , potentially confusing iterator dereferencing doing inside loop:

node * n = *i; cout << n->data << endl; 

more generally, if have somecontainer<sometype>, this:

for( somecontainer<sometype>::iterator = foo.begin(); != foo.end(); i++ ) {     sometype & val = *i;     // ... } 

the above approach particularly handy when iterating through map , have use i->second data instead of *i.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -