Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.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++ 使用一个功能打印地图和无序的_地图对_C++_C++11_Templates - Fatal编程技术网

C++ 使用一个功能打印地图和无序的_地图对

C++ 使用一个功能打印地图和无序的_地图对,c++,c++11,templates,C++,C++11,Templates,我已经有了这个工作代码: template <typename T1, typename T2> std::ostream& operator<<(std::ostream &out, std::map<T1, T2> &map){ for (auto it = map.begin(); it != map.end(); ++it) { out << it-> firs

我已经有了这个工作代码:

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream &out, std::map<T1, T2> &map){
        for (auto it = map.begin(); it != map.end(); ++it) {
                out <<  it-> first << ", " << it->second << '\n';
        }
        return out;
}
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream &out, std::unordered_map<T1, T2> &map){
        for (auto it = map.begin(); it != map.end(); ++it) {
                out <<  it-> first << ", " << it->second << '\n';
        }
        return out;
}
模板

std::ostream&operator您确实可以:这里有一个函数模板,用于任何具有
值的iterable类型,即
std::pair
。这不仅适用于
std::map
std::unordered\u map
,还适用于
std::vector
boost::container::list
等:

namespace detail {
    template<typename>
    struct is_pair : std::false_type { };

    template<typename T1, typename T2>
    struct is_pair<std::pair<T1, T2>> : std::true_type { };
}

template<
    // collection type
    typename T,
    // ensure value_type exists
    typename VT = typename T::value_type,
    // ensure value_type is some std::pair<>
    typename std::enable_if<detail::is_pair<VT>{}>::type* = nullptr
>
auto operator <<(std::ostream& out, T const& coll)
// ensure begin(coll) and end(coll) are legal
-> decltype(void(begin(coll)), void(end(coll)), out) {
    for (auto it = begin(coll); it != end(coll); ++it) {
        out << it->first << ", " << it->second << '\n';
    }
    return out;
}
名称空间详细信息{
模板
结构是_对:std::false_类型{};
模板
结构是_对:std::true_类型{};
}
模板<
//集合类型
类型名T,
//确保值类型存在
typename VT=typename T::value\u type,
//确保值类型为std::pair
typename std::enable_if::type*=nullptr
>

自动操作员这是一个演示程序

#include <iostream>
#include <map>
#include <type_traits>
#include <utility>

template <class T1, class T2, class T3, class T4,
          template <class T1, class T2, class T3, class T4> class Container>

std::ostream & operator<<( std::ostream &out, const Container<T1, T2, T3, T4> &c )
{
    static_assert( ( std::is_same<typename Container<T1, T2, T3, T4>::value_type, 
                     std::pair<const T1, T2>>::value ), "Invalid value type of the Container" );
    for ( const auto &p : c ) 
    {
        out <<  p.first << ", " << p.second << '\n';
    }

    return out;
}


int main() 
{
    std::map<int, char> m = 
    {
        { 65, 'A' }, { 66, 'B' }, { 67, 'C' }
    };

    std::cout << m << std::endl;

    std::multimap<int, char> mm = 
    {
        { 65, 'A' }, { 66, 'B' }, { 67, 'C' }
    };

    std::cout << mm << std::endl;

    return 0;
}

编写第三个模板,它只接受一个泛型
typename T
参数,并从这两个参数中调用它。您能再解释一下解决方案吗?你能给我指出一些更好的资源来更好地理解C++模板吗?@ DV1729:你需要缩小你的请求并澄清你需要解释的方面。是这里使用的唯一技术,有几十个高质量的答案,因此可以深入解释SFINAE;这可能是一个很好的起点(尽管看起来有点过时)。
65, A
66, B
67, C

65, A
66, B
67, C