对于常见数据类型通用打印支持 整形 浮点型 字符串类型,char vector 包括多维度数组 map set T ah[N] 类型数组,包括多维数组 array<T, N> 和 多维度数组*
main.cpp
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;
template <typename T>
concept isPrintable = requires(T t) {
cout << t;
};
template <typename T>
concept LoopAble = requires(T t) {
++t.begin();
*(t.begin());
t.end();
t.begin() != t.end();
};
template <typename T>
concept PairAble = requires(T&& t) {
t.first;
t.second;
};
template <typename T>
concept ArrayLike = requires(T t) {
size(t)->size_t;
};
void printer(char* buffer, bool end = true) {
cout << buffer << "\t";
if (end) {
cout << endl;
}
}
void printer(const string& buffer, bool end = true) {
cout << buffer << "\t";
if (end) {
cout << endl;
}
}
template <template <typename, size_t> typename Array, typename T, size_t N>
void printer(Array<T, N>& arr, bool end = true);
template <typename T>
void printer(T&& v, bool end = true) requires LoopAble<T> || is_array_v<remove_reference_t<T>> || isPrintable<T> || PairAble<T> {
if constexpr (is_array_v<remove_reference_t<T>>) {
for (int i = 0; i < size(v); i++) {
printer(v[i], false);
}
} else if constexpr (PairAble<remove_reference_t<decltype(v)>>) {
printer(v.first, false);
printer(v.second, false);
} else if constexpr (LoopAble<remove_reference_t<decltype(v)>>) {
for (auto& one : v) {
printer(one, false);
}
} else if constexpr (isPrintable<decay_t<decltype(v)>>) {
cout << v << " ";
}
if (end) {
cout << endl;
}
};
template <template <typename, size_t> typename Array, typename T, size_t N>
void printer(Array<T, N>& arr, bool end) {
for (int i = 0; i < size(arr); i++) {
printer(arr[i], false);
}
if (end) {
cout << endl;
}
}
int main() {
int x = 1;
printer(x);
vector<int> vec{1, 2, 3, 4, 5, 6, 7};
printer(vec);
int arr[5] = {1, 2, 3, 4, 5};
printer(arr);
int tarr[2][5] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};
printer(tarr);
vector<vector<int>> vt{{1, 2, 3}, {2, 3, 4}, {4, 5, 6}};
printer(vt);
vector<vector<vector<int>>> vtt{{{1, 2, 3}, {2, 3, 4}}, {{4, 5, 6}, {5, 6, 7}}};
printer(vtt);
char buffer[] = "hello world";
printer(buffer);
map<int, int> mp;
mp[1] = 1;
printer(mp);
set<string> ss{"hello", "world"};
printer(ss);
array<int, 5> ar{1, 2, 3, 4, 5};
printer(ar);
vector<array<int, 6>> varr{{1, 2, 3, 4, 5, 6}, {1, 2, 3, 4, 5, 6}};
printer(varr);
array<array<int, 1>, 1> arra{{1}};
printer(arra);
}
输出
g++ main.cpp -std=c++20
|