1

これを for ループに書き直したいと思います (動作します)

for (vector<shared_ptr<Node<int>>>::iterator it = n.root.children.begin();
    it != n.root.children.end(); 
    ++it) 
    {cout << (*it)->value<<flush;}

範囲ベースの for ループに変換します。私が試したのは

for (shared_ptr<Node<int>> child:(n.root).children){
       cout<<(child->value)<<" "<<flush;
    }

しかし、それは私にコアダンプを与えます。ルートは Node タイプです

template <typename T>
class Node{
public:
    T value;
    vector<shared_ptr<Node>> children;
};

main.cpp のこれらの行は正常に機能します。

cout<<(n.root).value<<flush;
cout<<n.root.children.front()->value<<flush;

g++ 4.7.2 を使用しています。

4

1 に答える 1

2

どうぞ。これを試して。

for (auto v : n.root.children ) {
    cout << v->value << flush;
}
于 2012-10-08T18:54:43.297 に答える