pythonのprintみたいにvector,map,set,arrayの中身をc++で出力する

int, string, pair...
array, array< array >
vector, vector< vector >
set, unordered_set
map, unordered_map
生配列未対応

#include <string> #include <array> #include <vector> #include <cstdlib> #include <sstream> #include <iostream> #include <set> #include <unordered_set> #include <map> #include <unordered_map> using namespace std; template<class F, class S> string in_v_to_str (const pair<F, S> v); template<class F, class S> string v_to_str (const pair<F, S> v); string in_v_to_str (const char v) { return "'" + string{v} + "'"; } string in_v_to_str (const char* v) { return "\"" + string(v) + "\""; } string in_v_to_str (const string v) { return "\"" + v + "\""; } template<class T> string in_v_to_str (const T v) { stringstream ss; ss << v; return ss.str(); } template<class T> string v_to_str (const T v) { stringstream ss; ss << v; return ss.str(); } template<class T, size_t N> string v_to_str (const array<T, N>& v) { stringstream ss; if (v.size() > 0) { ss << "["; for (int i = 0; i < v.size() - 1; ++i) { ss << in_v_to_str(v[i]) << ", "; } ss << in_v_to_str(v[v.size() - 1]) << "]"; } else { ss << "[]"; } return ss.str(); } template<class T, size_t N> string v_to_str (const array< array<T, N>, N >& v) { stringstream ss; if (v.size() > 0) { ss << "["; for (int i = 0; i < v.size() - 1; ++i) { ss << v_to_str(v[i]) << ", "; } ss << v_to_str(v[v.size() - 1]) << "]"; } else { ss << "[-]"; } return ss.str(); } template<class T> string v_to_str (const vector<T>& v) { stringstream ss; if (v.size() > 0) { ss << "["; for (int i = 0; i < v.size() - 1; ++i) { ss << in_v_to_str(v[i]) << ", "; } ss << in_v_to_str(v[v.size() - 1]) << "]"; } else { ss << "[]"; } return ss.str(); } template<class T> string v_to_str (const vector< vector<T> >& v) { stringstream ss; if (v.size() > 0) { ss << "["; for (int i = 0; i < v.size() - 1; ++i) { ss << v_to_str(v[i]) << ", "; } ss << v_to_str(v[v.size() - 1]) << "]"; } else { ss << "[-]"; } return ss.str(); } template<class T> string v_to_str (const set<T>& v) { stringstream ss; int len = v.size(); ss << (v.size() > 0 ? "{" : "{}"); for (auto& i : v) { ss << in_v_to_str(i) << (len-- > 1 ? ", " : "}"); } return ss.str(); } template<class K, class V> string v_to_str (const map<K, V>& v) { stringstream ss; int len = v.size(); ss << (v.size() > 0 ? "{" : "{}"); for (auto& i : v) { ss << in_v_to_str(i.first) << " : " << in_v_to_str(i.second) << (len-- > 1 ? ", " : "}"); } return ss.str(); } template<class T> string v_to_str (const unordered_set<T>& v) { stringstream ss; int len = v.size(); ss << (v.size() > 0 ? "{" : "{}"); for (auto& i : v) { ss << in_v_to_str(i) << (len-- > 1 ? ", " : "}"); } return ss.str(); } template<class K, class V> string v_to_str (const unordered_map<K, V>& v) { stringstream ss; int len = v.size(); ss << (v.size() > 0 ? "{" : "{}"); for (auto& i : v) { ss << in_v_to_str(i.first) << " : " << in_v_to_str(i.second) << (len-- > 1 ? ", " : "}"); } return ss.str(); } template<class F, class S> string in_v_to_str (const pair<F, S> v) { stringstream ss; ss << "<" << v_to_str(v.first) << ", " << v_to_str(v.second) << ">"; return ss.str(); } template<class F, class S> string v_to_str (const pair<F, S> v) { stringstream ss; ss << "<" << v_to_str(v.first) << ", " << v_to_str(v.second) << ">"; return ss.str(); } string print () { return ""; } template<typename F, typename... R> string print (const F& f, const R& ...r) { stringstream ss; ss << v_to_str(f); if (sizeof...(r) > 0) { ss << " " << print(r...); } return ss.str(); } int main () { vector<string> vs = {"a", "b"}; vector< vector<string> > vvs = {{"a", "b"}, {"c", "d"}}; array<string, 2> as = {"a", "b"}; array< array<string, 2>, 2> aas = {array<string, 2>{"a", "b"}, array<string, 2>{"c", "d"}}; unordered_set<string> uss = {"s1", "s2"}; unordered_map<string, int> umsi = {{"k1", 1}, {"k2", 2}}; unordered_map<string, char> umsc = {{"k1", '1'}, {"k2", '2'}}; cout << print(vs, vvs, as, aas, uss, umsi, umsc, "s", 'c', 1) << endl; // ["a", "b"] [["a", "b"], ["c", "d"]] ["a", "b"] [["a", "b"], ["c", "d"]] {"s2", "s1"} {"k2" : 2, "k1" : 1} {"k2" : '2', "k1" : '1'} s c 1 vector< vector< vector<int> > > vvvi; cout << print(vvvi) << endl; // [-] vvvi = {{{}, {}}, {{}, {}}, {{}, {}}}; cout << print(vvvi) << endl; // [[[], []], [[], []], [[], []]] vector< pair<int, int> > vpii(5, pair<int, int>(5, 55)); cout << print(vpii) << endl; // [<5, 55>, <5, 55>, <5, 55>, <5, 55>, <5, 55>] vector< pair< int , vector<int> > > t; t.emplace_back(pair< int , vector<int> >(5, vector<int>(3, 126))); cout << print(t) << endl; return 0; }

コメント