【发布时间】:2012-01-14 20:10:40
【问题描述】:
当我有一个包含模板映射的模板类和typedef 在以下代码中声明的const_iterator 时,我如何遍历类外部的映射元素,主要是 fe 以打印它们输出?
template<class K, class V>
class template_map{
private:
typedef typename std::map<K,V> TMap;
TMap my_map;
public:
typedef typename TMap::const_iterator const_iterator;
...
};
int main()
{
template_Map<int,double> Map1 //suppose that contains elements
?
}
更新: typedef 迭代器可以在类外使用吗?如果是,以什么方式?
【问题讨论】:
-
你的主要功能有误。例如,完整的类型是
template_map<int,double>。然后你可以说template_map<int,double>::const_iterator来命名你的迭代器。您还必须在template_map<K,V>的公共接口中公开my_map的迭代器函数。
标签: c++ templates map iterator