我有一堂课如下:
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。据我了解,转换迭代器仍然需要您在模板参数中提供底层容器迭代器。那么有没有一种方法可以使用转换迭代器围绕地图键定义自定义迭代器类型,同时使其适用于不同类型的地图容器?