私は次のクラスを持っています:
#include <unordered_map>
#include <memory>
class Node {
public:
typedef std::unique_ptr<Node> ptr_type;
typedef std::unordered_map<char, ptr_type> map_type;
typedef /**???**/ const_iterator;
const_iterator begin() const;
const_iterator end() const;
private:
map_type _children;
};
ご覧のとおり、このクラスのユーザーが要素_children
を変更せずに繰り返し処理できる方法が必要です。pair<char, const Node&>
の代わりに型の要素を指すイテレータを作成したいのはそのためですpair<char, ptr_type>
。
基本イテレータ クラスの作成は、目の前のタスクには少し複雑すぎるようです。transform_iterator
私はブーストイテレータを見てきましたが、それが進むべき道かもしれないと思いますが、それを機能させる方法をまだ見つけていません。
で定義されているイテレータのさまざまな例の例をどこで見つけることができるか知っている人はいますboost-iterators
か? ドキュメントには各タイプの例が 1 つしかなく、常に私のニーズに合うとは限りません (私はこのライブラリを初めて使用するため、明らかな何かを見落としている可能性があります)。
更新:これが私の使用の試みですboost::transform_iterator
class Node {
public:
typedef std::unique_ptr<Node> ptr_type;
typedef std::unordered_map<char, ptr_type> map_type;
struct Transformer {
std::pair<char, const Node&> operator()(const std::pair<char, ptr_type> &p) const {
return std::pair<char, const Node&>(p.first, *p.second);
}
};
typedef boost::transform_iterator<Transformer, map_type::const_iterator, std::pair<char, const Node&>&, std::pair<char, const Node&>> const_iterator;
const_iterator begin() const {
return boost::make_transform_iterator<Transformer, map_type::const_iterator>(_children.begin(), Transformer());
}
const_iterator end() const {
return boost::make_transform_iterator<Transformer, map_type::const_iterator>(_children.end(), Transformer());
}
private:
map_type _children;
};
残念ながらコンパイルされず、次のエラーが発生します。
error: no type named ‘type’ in ‘boost::mpl::eval_if<boost::is_same<boost::iterators::use_default, boost::iterators::use_default>, boost::result_of<const Node::Transformer(const std::pair<const char, std::unique_ptr<Node> >&)>, boost::mpl::identity<boost::iterators::use_default> >::f_ {aka struct boost::result_of<const Node::Transformer(const std::pair<const char, std::unique_ptr<Node> >&)>}’
typedef typename f_::type type;