C++ 显示具有 vector 的 map

标签 c++ string vector std-pair

std::map<std::string, std::vector<string>> data;

为了使用 copy 打印出来,我的std::ostream_iterator应该怎么办?是吗?

显然 std::ostream_iterator<std::pair<std::string, std::vector<std::string>>> out_it(std::cout, "\n");没有成功。

我的 operator<<过载如下 std::ostream& operator<<(std::ostream& out, const std::pair<std::string, std::vector<std::string>>& p)并写出 p.firstp.second并返回它。

最佳答案

如果您使用 C++ 进行任何认真的编程,您最终将需要一种通用的方法来打印集合。

这是一个基础:

#include <iostream>
#include <map>
#include <vector>
#include <string>


// introduce the concept of an object that emits values to an ostream
// by default it simply calls operator <<
template<class T> struct emitter
{
    using arg_type = T;

    emitter(const T& v) : v_(v) {}

    friend std::ostream& operator<<(std::ostream& os, const emitter& e) {
        return os << e.v_;
    }

    const T& v_;
};

// introduce the concept of an io manipulator called emit
template<class T> auto emit(const T& v) -> emitter<T>
{
    return emitter<std::decay_t<T>>(v);
}

// specialise the emitter for maps
template<class K, class V, class C, class A>
struct emitter<std::map<K, V, C, A>>
{
    using arg_type = std::map<K, V, C, A>;

    emitter(const arg_type& v) : v_(v) {}

    friend std::ostream& operator<<(std::ostream& os, const emitter& e) {
        const char* elem_sep = "\n\t";
        const char* end_sep = " ";
        os << "{";
        for (const auto& elem : e.v_)
        {
            os << elem_sep << emit(elem.first) << ": " << emit(elem.second);
            end_sep = "\n";
        }

        return os << end_sep << "}";
    }

    const arg_type& v_;
};

// specialise the emitter for vectors
template<class V, class A>
struct emitter<std::vector<V, A>>
{
    using arg_type = std::vector<V, A>;

    emitter(const arg_type& v) : v_(v) {}

    friend std::ostream& operator<<(std::ostream& os, const emitter& e) {
        const char* elem_sep = " ";
        const char* end_sep = " ";
        os << "[";
        for (const auto& elem : e.v_)
        {
            os << elem_sep << emit(elem);
            elem_sep = ", ";
        }

        return os << end_sep << "]";
    }

    const arg_type& v_;
};


int main() {
    // build test data
    std::map<std::string, std::vector<std::string>> data;

    data.emplace("a", std::vector<std::string>{ "now", "is", "the", "time" });
    data.emplace("b", std::vector<std::string>{ "for", "all", "good", "men" });
    data.emplace("c", std::vector<std::string>{ "to", "come", "to", "the" });
    data.emplace("d", std::vector<std::string>{ "aid", "of", "their", "party" });

    // request an emitter manipulator
    std::cout << emit(data) << std::endl;
}

预期输出:

{
    a: [ now, is, the, time ]
    b: [ for, all, good, men ]
    c: [ to, come, to, the ]
    d: [ aid, of, their, party ]
}

关于C++ 显示具有 vector 的 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41097518/

相关文章:

c++ - 什么是 std::identity 以及它是如何使用的?

java - 正则表达式从字符串中的字母字符之后删除字符

python - 在python中将unicode西里尔符号转换为字符串

c++ - 通过 id 并返回名称在 vector 中搜索结构元素

命名空间中的 C++ std vector 初始化

c++ - 计算分数和总问题

C++ STL Map - find(pair<string,bool>) 在 Windows Visual Studio 中有效,但在 Linux 中不适用于 g++!

c++ - 变换 Mat 矩阵

java - 使用replaceAll替换数字中的字符在java中不起作用

c++ - 具有 pop_back 函数的类模板 vector 无法按预期工作