C++模拟python风格的print函数--打印vector,map,list等结构

    // 最基本实现
    template<typename T>
    static void print(T t) {
        std::cout << t;
    }

    // 处理 std::pair
    template<typename Kt, typename Vt>
    static void print(std::pair<Kt, Vt> kv) {
        print(kv.first);
        print(" = ");
        print(kv.second);
    }

    // 对 std::string 特殊处理
    // 注释掉可以按照序列输出
    static void print(std::string s) {
        std::cout << s;
    }

    //! 处理各种序列容器(vector/list/map ...)
    template<typename T, typename AllocT, template<typename, typename...> typename SequenceT>
    static void print(SequenceT<T, AllocT> seq) {
        print("{");
        for (auto iter = std::begin(seq); iter != std::end(seq); iter = std::next(iter)) {
            print(*iter);
            print(",");
        }
        print("\b}");
        print("\n");
    }
posted @ 2021-03-02 11:28  Xu_Lin  阅读(378)  评论(0编辑  收藏  举报