Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
adapter.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 <deque>
36#include <iostream>
37#include <queue>
38#include <stack>
39#include <string>
40#include <utility>
41#include <vector>
42
43namespace console {
58 template <class T, class Container = std::deque<T>>
59 class Stack : public std::stack<T, Container> {
60 using base_type = std::stack<T, Container>;
61
62 public:
64 using base_type::stack;
65
74 typename base_type::value_type pop() {
75 auto top = std::move(base_type::top());
76 base_type::pop();
77 return top;
78 }
79
87 template <class CharT, class Traits>
88 friend std::basic_ostream<CharT, Traits> &
89 operator<<(std::basic_ostream<CharT, Traits> &os, const Stack &stack) {
90 return os << stack.c;
91 }
92 };
93
108 template <class T, class Container = std::deque<T>>
109 class Queue : public std::queue<T, Container> {
110 using base_type = std::queue<T, Container>;
111
112 public:
114 using base_type::queue;
115
124 typename base_type::value_type pop() {
125 auto top = std::move(base_type::front());
126 base_type::pop();
127 return top;
128 }
129
137 template <class CharT, class Traits>
138 friend std::basic_ostream<CharT, Traits> &
139 operator<<(std::basic_ostream<CharT, Traits> &os, const Queue &queue) {
140 return os << queue.c;
141 }
142 };
143
160 template <class T,
161 class Container = std::vector<T>,
162 class Compare = std::less<T>>
163 class PriorityQueue : public std::priority_queue<T, Container, Compare> {
164 using base_type = std::priority_queue<T, Container, Compare>;
165
166 public:
168 using base_type::priority_queue;
169
178 typename base_type::value_type pop() {
179 auto top = std::move(base_type::top());
180 base_type::pop();
181 return top;
182 }
183
191 template <class CharT, class Traits>
192 friend std::basic_ostream<CharT, Traits> &operator<<(
193 std::basic_ostream<CharT, Traits> &os, const PriorityQueue &pq) {
194 return os << pq.c;
195 }
196 };
197}
优先队列容器适配器,扩展自 std::priority_queue。
Definition adapter.h:163
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const PriorityQueue &pq)
重载输出流运算符,将栈的内容输出到流中。
Definition adapter.h:192
base_type::value_type pop()
弹出优先级最高的元素并返回其值。
Definition adapter.h:178
std::priority_queue< T, Container, Compare > base_type
Definition adapter.h:164
队列容器适配器,扩展自 std::queue。
Definition adapter.h:109
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const Queue &queue)
重载输出流运算符,将栈的内容输出到流中。
Definition adapter.h:139
std::queue< T, Container > base_type
Definition adapter.h:110
base_type::value_type pop()
弹出队首元素并返回其值。
Definition adapter.h:124
栈容器适配器,扩展自 std::stack。
Definition adapter.h:59
base_type::value_type pop()
弹出栈顶元素并返回其值。
Definition adapter.h:74
std::stack< T, Container > base_type
Definition adapter.h:60
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const Stack &stack)
重载输出流运算符,将栈的内容输出到流中。
Definition adapter.h:89
本库所有组件所在的顶层命名空间。