我正在研究一個非二叉樹結構,因此,我有一個結構定義如下,其中有一個數據,它的所有孩子都在一個名爲child的向量中。迭代結構中定義的向量
struct node{
string data;
vector< node* > child;
vector<node*>::iterator i
int Count;
};
我有我已經定義了打印的孩子在載體另一個函數,但我不能讓迭代工作
void printTree(node* &a){
for(a->i = a->child.begin(); a->i !=a->child.end();++i)
cout << *(a->i)->data <<endl;
}
迭代器是不是我得到一個錯誤在調用printTree時定義。我嘗試在printTree函數內部定義迭代器,但是我一直收到錯誤。有關如何更改我的代碼的任何建議?
爲什麼你需要每個節點的迭代器? –
有沒有理由讓你的節點裏面有迭代器? – Joe
可能不是解決方案,但在for循環中,您正在增加'i'而不是'a-> i'。 – 0x499602D2