标准库地图和模板功能
std library map and template functions
我真的不明白为什么这段代码
#include <map>
template<typename T, typename U> std::ostream& operator<<(std::ostream& o,
const std::map<T,U>& input)
{
for (std::map<typename T,typename U>::iterator it=input.begin(); it!=input.end(); ++it)
{
o << it->first << " => " << it->second << 'n';
}
return o;
}
返回此编译错误:
error: wrong number of template arguments (1, should be 4)
error: provided for ‘template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map’
谁能帮我??
你应该在迭代器声明之前写 typename,并使用const_iterator:
for (typename std::map<T,U>::const_iterator it=input.begin(); it!=input.end(); ++it
运算符<<的参数需要 const 对象。因此,地图的元素必须是恒定的。要实现这一点,请使用const_iterator。迭代器声明中的类型名是必需的,以指示以下表达式是嵌套模板类,具体取决于类型 T 和 U。
另请参阅此问题:理解函数模板的参数
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 是否有任何C++功能可以对地图进行排序?
- 在地图中存储的类中调用功能
- 使用提升功能与地图绑定提升
- 标准::地图比较功能
- 使用C++模板实现 Haskell 的"地图"功能时出现问题
- 在地图中对匹配对执行功能
- C 将类成员功能添加到地图中
- 订购设置/地图的计数功能
- 在C 中的地图中访问对象的成员功能
- 分割故障,可能的原因:功能指针,多线程,STL地图等
- 在此地图中,取消运算符的目的和功能是什么
- 指针的地图指向不同返回类型和签名的功能
- 具有多种比较功能的单标::地图/多地图
- 标准库地图和模板功能
- 地图的查找功能得到错误的结果
- 使用地图功能ubuntu
- 是否可以将STL复制功能与地图一起使用
- C++ 多重地图插入功能