Home > libalgo > STL コンテナの ostream 出力

STL コンテナの ostream 出力

マクロ楽しい ✌(‘ω’✌ )三✌(‘ω’)✌三( ✌’ω’)✌

namespace {
#define __DECLARE__(C)    \
    template <typename T> \
    std::ostream &operator<<(std::ostream &, const C<T> &);

#define __DECLAREM__(C)               \
    template <typename T, typename U> \
    std::ostream &operator<<(std::ostream &, const C<T, U> &);

__DECLARE__(std::vector)
__DECLARE__(std::deque)
__DECLARE__(std::set)
__DECLARE__(std::stack)
__DECLARE__(std::queue)
__DECLARE__(std::priority_queue)
__DECLARE__(std::unordered_set)
__DECLAREM__(std::map)
__DECLAREM__(std::unordered_map)

template <typename T, typename U>
std::ostream &operator<<(std::ostream &, const std::pair<T, U> &);
template <typename... T>
std::ostream &operator<<(std::ostream &, const std::tuple<T...> &);
template <typename T, std::size_t N>
std::ostream &operator<<(std::ostream &, const std::array<T, N> &);

template <typename Tuple, std::size_t N>
struct __TuplePrinter__ {
    static void print(std::ostream &os, const Tuple &t) {
        __TuplePrinter__<Tuple, N - 1>::print(os, t);
        os << ", " << std::get<N - 1>(t);
    }
};

template <typename Tuple>
struct __TuplePrinter__<Tuple, 1> {
    static void print(std::ostream &os, const Tuple &t) { os << std::get<0>(t); }
};

template <typename... T>
std::ostream &operator<<(std::ostream &os, const std::tuple<T...> &t) {
    os << '(';
    __TuplePrinter__<decltype(t), sizeof...(T)>::print(os, t);
    os << ')';
    return os;
}

template <typename T, typename U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &v) {
    return os << '(' << v.first << ", " << v.second << ')';
}

#define __INNER__                             \
    os << '[';                                \
    for (auto it = begin(c); it != end(c);) { \
        os << *it;                            \
        os << (++it != end(c) ? ", " : "");   \
    }                                         \
    return os << ']';

template <typename T, std::size_t N>
std::ostream &operator<<(std::ostream &os, const std::array<T, N> &c) {
    __INNER__
}

#define __DEFINE__(C)                                           \
    template <typename T>                                       \
    std::ostream &operator<<(std::ostream &os, const C<T> &c) { \
        __INNER__                                               \
    }

#define __DEFINEM__(C)                                             \
    template <typename T, typename U>                              \
    std::ostream &operator<<(std::ostream &os, const C<T, U> &c) { \
        __INNER__                                                  \
    }

#define __DEFINEW__(C, M1, M2)                                  \
    template <typename T>                                       \
    std::ostream &operator<<(std::ostream &os, const C<T> &c) { \
        std::deque<T> v;                                        \
        for (auto d = c; !d.empty(); d.pop()) v.M1(d.M2());     \
        return os << v;                                         \
    }

__DEFINE__(std::vector)
__DEFINE__(std::deque)
__DEFINE__(std::set)
__DEFINEW__(std::stack, push_front, top)
__DEFINEW__(std::queue, push_back, front)
__DEFINEW__(std::priority_queue, push_front, top)
__DEFINE__(std::unordered_set)
__DEFINEM__(std::map)
__DEFINEM__(std::unordered_map)
}

テスト

using namespace std;

int main() {
    array<int, 10> a;
    a.fill(-1);
    cout << a << endl;

    unordered_map<int, int> b = {{3, 1}, {33, 2}, {33, 5}, {4, 1}};
    cout << b << endl;

    vector<int> c = {1, 2, 1, 2};
    cout << c << endl;

    deque<int> d = {1, 2, 1, 2};
    cout << d << endl;

    vector<pair<int, int>> e = {{0, 1}, {1, 2}};
    cout << e << endl;

    pair<vector<int>, vector<int>> f = {{0, 1}, {1, 2}};
    cout << f << endl;

    stack<int> g;
    g.push(1);
    g.push(7);
    g.push(5);
    cout << g << endl;

    queue<int> h;
    h.push(1);
    h.push(7);
    h.push(5);
    cout << h << endl;

    priority_queue<int> i;
    i.push(1);
    i.push(7);
    i.push(5);
    cout << i << endl;

    using p = pair<int, string>;
    using q = map<vector<vector<int>>, int>;
    using r = array<int, 5>;
    p x = make_pair(10, "aho");
    q y = {{{{101, 102, 103}, {104, 105, 106}}, 100}, {{{201, 202, 203}, {204, 205, 206}}, 200}};
    r z = {{1, 4, 1, 4, 2}};
    auto j = make_tuple(x, y, z);
    cout << j << endl;
}