Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std库映射和模板函数_C++_Dictionary - Fatal编程技术网

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’