Console Library 4.7.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
output.h
Go to the documentation of this file.
1
11
12/*
13Copyright (c) 2026 MrXie1109
14
15Permission is hereby granted, free of charge, to any person obtaining a copy
16of this software and associated documentation files (the "Software"), to deal
17in the Software without restriction, including without limitation the rights
18to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19copies of the Software, and to permit persons to whom the Software is
20furnished to do so, subject to the following conditions:
21
22The above copyright notice and this permission notice shall be included in all
23copies or substantial portions of the Software.
24
25THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31SOFTWARE.
32*/
33
34#pragma once
35#include <cstddef>
36#include <iterator>
37#include "repr.h"
38
39namespace console
40{
46
54 template <class Cont>
55 std::ostream &cont_print_sequence(std::ostream &os, const Cont &cont)
56 {
57 if (begin(cont) == end(cont))
58 return os << "[]";
59 auto it = begin(cont);
60 os << '[';
61 repr(*it, os);
62 while (++it != end(cont))
63 {
64 os << ", ";
65 repr(*it, os);
66 }
67 return os << ']';
68 }
69
77 template <class Cont>
78 std::ostream &cont_print_set(std::ostream &os, const Cont &cont)
79 {
80 if (begin(cont) == end(cont))
81 return os << "{}";
82 auto it = begin(cont);
83 os << '{';
84 repr(*it, os);
85 while (++it != end(cont))
86 {
87 os << ", ";
88 repr(*it, os);
89 }
90 return os << '}';
91 }
92
100 template <class Cont>
101 std::ostream &cont_print_map(std::ostream &os, const Cont &cont)
102 {
103 if (begin(cont) == end(cont))
104 return os << "{}";
105 auto it = begin(cont);
106 os << '{';
107 repr(it->first, os);
108 os << ": ";
109 repr(it->second, os);
110 while (++it != end(cont))
111 {
112 os << ", ";
113 repr(it->first, os);
114 os << ": ";
115 repr(it->second, os);
116 }
117 return os << '}';
118 }
119
121
127
133 template <class Tuple, size_t N = std::tuple_size<Tuple>::value>
135 {
141 static void print(std::ostream &os, const Tuple &t)
142 {
144 if (N > 1)
145 os << ", ";
146 repr(std::get<N - 1>(t), os);
147 }
148 };
149
153 template <class Tuple>
154 struct TuplePrinter<Tuple, 1>
155 {
156 static void print(std::ostream &os, const Tuple &t)
157 {
158 repr(std::get<0>(t), os);
159 }
160 };
161
165 template <class Tuple>
166 struct TuplePrinter<Tuple, 0>
167 {
168 static void print(std::ostream &, const Tuple &) {}
169 };
170
172
173 // ==================== 标准容器 operator<< 重载 ====================
174
180
182 template <class T>
183 std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec)
184 {
185 return cont_print_sequence(os, vec);
186 }
187
189 template <class T>
190 std::ostream &operator<<(std::ostream &os, const std::deque<T> &deq)
191 {
192 return cont_print_sequence(os, deq);
193 }
194
196 template <class T>
197 std::ostream &operator<<(std::ostream &os, const std::list<T> &lst)
198 {
199 return cont_print_sequence(os, lst);
200 }
201
203 template <class T>
204 std::ostream &operator<<(std::ostream &os, const std::forward_list<T> &flst)
205 {
206 return cont_print_sequence(os, flst);
207 }
208
210 template <class T, size_t n>
211 std::ostream &operator<<(std::ostream &os, const std::array<T, n> &arr)
212 {
213 return cont_print_sequence(os, arr);
214 }
215
217 template <class T>
218 std::ostream &operator<<(std::ostream &os, const std::set<T> &s)
219 {
220 return cont_print_set(os, s);
221 }
222
224 template <class K, class V>
225 std::ostream &operator<<(std::ostream &os, const std::map<K, V> &m)
226 {
227 return cont_print_map(os, m);
228 }
229
231 template <class T>
232 std::ostream &operator<<(std::ostream &os, const std::multiset<T> &ms)
233 {
234 return cont_print_set(os, ms);
235 }
236
238 template <class K, class V>
239 std::ostream &operator<<(std::ostream &os, const std::multimap<K, V> &mm)
240 {
241 return cont_print_map(os, mm);
242 }
243
245 template <class T>
246 std::ostream &operator<<(std::ostream &os, const std::unordered_set<T> &us)
247 {
248 return cont_print_set(os, us);
249 }
250
252 template <class K, class V>
253 std::ostream &operator<<(std::ostream &os,
254 const std::unordered_map<K, V> &um)
255 {
256 return cont_print_map(os, um);
257 }
258
260 template <class T>
261 std::ostream &operator<<(std::ostream &os,
262 const std::unordered_multiset<T> &ums)
263 {
264 return cont_print_set(os, ums);
265 }
266
268 template <class K, class V>
269 std::ostream &operator<<(std::ostream &os,
270 const std::unordered_multimap<K, V> &ump)
271 {
272 return cont_print_map(os, ump);
273 }
274
276 template <class T>
277 std::ostream &operator<<(std::ostream &os, const std::valarray<T> &va)
278 {
279 return cont_print_sequence(os, va);
280 }
281
290 template <class T, class U>
291 std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p)
292 {
293 os << '(';
294 repr(p.first, os);
295 os << ", ";
296 repr(p.second, os);
297 return os << ')';
298 }
299
307 template <class... Args>
308 std::ostream &operator<<(std::ostream &os, const std::tuple<Args...> &t)
309 {
310 os << "(";
311 TuplePrinter<std::tuple<Args...>>::print(os, t);
312 return os << ")";
313 }
314
316
322
330 template <class T, size_t N>
331 std::array<T, N> to_array(const T (&ar)[N])
332 {
333 std::array<T, N> arr;
334 std::copy(ar, ar + N, arr.begin());
335 return arr;
336 }
337
345 template <class T, size_t N>
346 std::vector<T> to_vector(const T (&ar)[N])
347 {
348 std::vector<T> vec(N);
349 std::copy(ar, ar + N, vec.begin());
350 return vec;
351 }
352
354
370 class Output
371 {
372 std::ostream &os;
373 std::string sep;
374 std::string end;
375 bool isFlush;
376
377 public:
381 Output() : os(std::cout), sep(" "), end("\n"), isFlush(true) {}
382
390 Output(std::ostream &o, const std::string &s, const std::string &e, bool isF)
391 : os(o), sep(s), end(e), isFlush(isF) {}
392
398 {
399 os << end;
400 if (isFlush)
401 os << std::flush;
402 return *this;
403 }
404
411 template <class T>
412 Output &operator()(const T &t)
413 {
414 os << t;
415 return operator()();
416 }
417
426 template <class T, class... Args>
427 Output &operator()(const T &t, const Args &...args)
428 {
429 os << t << sep;
430 return operator()(args...);
431 }
433}
Output & operator()(const T &t)
单参数调用:输出该参数后加结尾符。
Definition output.h:412
Output & operator()()
无参数调用:仅输出结尾符(如换行)。
Definition output.h:397
Output(std::ostream &o, const std::string &s, const std::string &e, bool isF)
自定义构造。
Definition output.h:390
Output & operator()(const T &t, const Args &...args)
多参数调用:依次输出每个参数,参数之间插入分隔符,最后加结尾符。
Definition output.h:427
Output()
默认构造,输出到 std::cout,分隔符为空格,结尾为换行,并刷新。
Definition output.h:381
std::array< T, N > to_array(const T(&ar)[N])
将 C 风格数组转换为 std::array。
Definition output.h:331
std::vector< T > to_vector(const T(&ar)[N])
将 C 风格数组转换为 std::vector。
Definition output.h:346
MultiArray< T, Dims... > operator<<(const MultiArray< T, Dims... > &a, const MultiArray< T, Dims... > &b)
左移(数组 << 数组)。
Definition multiarray.h:1227
std::ostream & cont_print_sequence(std::ostream &os, const Cont &cont)
输出序列容器(如 vector, list)的内容,格式为 [a, b, c]。
Definition output.h:55
std::ostream & cont_print_set(std::ostream &os, const Cont &cont)
输出集合容器(如 set, unordered_set)的内容,格式为 {a, b, c}。
Definition output.h:78
std::ostream & cont_print_map(std::ostream &os, const Cont &cont)
输出映射容器(如 map, unordered_map)的内容,格式为 {key: value, ...}。
Definition output.h:101
enable_if_string< T > repr(T &&value, std::ostream &os=std::cout)
输出字符串类型(std::string, const char* 等)的表示,带双引号。
Definition repr.h:79
本库所有组件所在的顶层命名空间。
class console::Output print
全局输出对象,模仿 Python 的 print 函数。
@ N
Definition kb.h:66
@ T
Definition kb.h:72
struct console::InputSettings cout
提供类似 Python 的 repr() 函数,用于生成对象的可读字符串表示。
static void print(std::ostream &, const Tuple &)
Definition output.h:168
static void print(std::ostream &os, const Tuple &t)
Definition output.h:156
递归打印 tuple 的辅助模板(主模板)。
Definition output.h:135
static void print(std::ostream &os, const Tuple &t)
递归打印 tuple 元素。
Definition output.h:141