C++ std库映射和模板函数
我真的不明白为什么这个代码C++ std库映射和模板函数,c++,dictionary,C++,Dictionary,我真的不明白为什么这个代码 #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
#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;
}
#包括
模板std::ostream&operator您应该在迭代器声明之前写入typename,并使用const_迭代器:
for(typename std::map::const_迭代器it=input.begin();it!=input.end();++it
运算符的参数您应该在迭代器声明之前写入typename,并使用const_迭代器:
for(typename std::map::const_迭代器it=input.begin();it!=input.end();++it
运算符的参数或者更好,使用auto
:for(auto it=input.begin();…
或者甚至基于范围的for循环:for(auto&pair:input)
或者更好,使用auto
:for(auto it=input.begin();…
或者甚至基于范围的for循环:for(auto&pair:input)
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’