我有一个类似以下内容的类:
class ArraySim{
public:
DataStructure* ds;
ArraySim(bool which){
if(true)
ds = new STDMap();
else
ds = new HashMap();
}
value_type& operator[](int idx){
return ds->getValAtIndex(idx);
}
//define a custom iterator type that can be used to iterate over both std::map and boost::unordered //map keys.
}
class DataStructure{
vitrual value_type& getValAtIndex(int idx)=0;
};
class STDMap: public DataStructure{
//Class that wraps a std::map object and implements the virtual method to return the value against a //particular index(key)
};
class HashMap: publlic DataStructure{
//Class that wraps a boost::unordered_map object and implements the virtual method to return the value //against a particular index(key)
} 我经历过:Generic Iterator和Transform Iterator。据我所知,转换迭代器仍然需要在模板参数中给出底层容器迭代器。那么,有没有一种方法可以使用转换迭代器来定义映射关键字周围的自定义迭代器类型,同时使其适用于不同类型的映射容器?
发布于 2012-11-30 18:50:36
如果你在使用Boost,你可以使用any_range。
typedef any_range<value_type, boost::forward_pass_traversal_tag,
value_type &, std::ptrdiff_t> range;
typedef any_range<value_type, boost::forward_pass_traversal_tag,
const value_type &, std::ptrdiff_t> const_range;
typedef range::iterator iterator;
typedef const_range::const_iterator const_iterator;
virtual iterator begin() = 0;
virtual iterator end() = 0;
virtual const_iterator begin() const = 0;
virtual const_iterator end() const = 0;您的begin和end虚拟只需要构造适当的迭代器:
iterator begin() { return iterator(object.begin()); }https://stackoverflow.com/questions/13643384
复制相似问题