#pragma once
#define FASTIO_H
#include <cmath> // 数学函数,用于浮点数处理
#include <cstring> // C 风格字符串操作
#include <string> // std::string 支持
#include <cstdint> // 固定宽度整数类型
namespace FastIO {
static constexpr size_t DEFAULT_BUFFER_SIZE = 1 << 16; // 默认缓冲区大小
static constexpr size_t MAX_NUMERIC_BUFFER_SIZE = 64; // 数值转换最大缓冲区大小
// IO 错误类型枚举
enum class IOError {
None = 0, // 无错误
EndOfFile, // 文件结束
InvalidInput, // 无效输入
OutOfRange, // 数值超出范围
IOError // IO 错误
};
// 输入流类
class istream {
private:
char* buffer; // 输入缓冲区
char* current; // 当前读取位置
char* end; // 缓冲区结束位置
const size_t bufferSize; // 缓冲区大小
IOError lastError = IOError::None; // 最后错误状态
// 填充缓冲区
[[nodiscard]] constexpr inline __attribute__((always_inline)) bool fillBuffer() noexcept {
size_t bytesRead = fread(buffer, 1, bufferSize, stdin); // 从标准输入读取数据到缓冲区
current = buffer; // 重置当前位置
end = buffer + bytesRead; // 设置结束位置
return bytesRead > 0; // 返回是否成功读取
}
// 获取一个字符(不移动指针)
[[nodiscard]] constexpr inline __attribute__((always_inline)) char peekChar() noexcept {
// 如果缓冲区为空且无法填充,返回 EOF
if (current == end && !fillBuffer()) {
return EOF;
}
return *current; // 返回当前字符
}
// 获取一个字符(移动指针)
[[nodiscard]] constexpr inline __attribute__((always_inline)) char getChar() noexcept {
// 如果缓冲区为空,尝试填充
if (current == end) {
if (!fillBuffer()) {
lastError = feof(stdin) ? IOError::EndOfFile : IOError::IOError; // 根据错误类型设置最后错误状态
return EOF; // 返回 EOF
}
}
return *current++; // 返回当前字符并移动指针
}
// 跳过空白字符
template <typename T>
constexpr inline __attribute__((always_inline)) void skipWhitespace() noexcept {
char ch;
// 循环读取,检查当前字符是否为空白字符或 EOF
while ((ch = peekChar()) != EOF && (ch == ' ' || ch == '\n' || ch == '\t' || ch == '\r' || ch == '\v' || ch == '\f')) {
++current; // 跳过空白字符
}
}
public:
// 构造函数,分配缓冲区
explicit istream(size_t bufSize = DEFAULT_BUFFER_SIZE) : buffer(new char[bufSize]), current(buffer), end(buffer), bufferSize(bufSize) {}
// 析构函数,释放缓冲区
~istream() noexcept {
delete[] buffer;
}
// 获取最后错误状态
[[nodiscard]] IOError error() const noexcept {
return lastError;
}
// 清除错误状态
void clear() noexcept {
lastError = IOError::None;
}
// 读取无符号整数
template <typename T, typename std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
inline __attribute__((always_inline)) istream & operator>>(T& x) noexcept {
clear(); // 清除错误状态
skipWhitespace<T>(); // 跳过空白字符
x = 0; // 初始化结果
char ch = getChar(); // 获取第一个非空白字符
if (ch == EOF) {
lastError = IOError::EndOfFile;
return *this;
}
// 跳过前导零
while (ch == '0') {
ch = getChar();
if (ch == EOF) {
lastError = IOError::EndOfFile;
x = 0;
return *this;
}
}
// 检查是否为数字
if (ch < '0' || ch > '9') {
lastError = IOError::InvalidInput;
--current; // 回退指针
return *this;
}
// 预计算最大值限制,防止溢出
T maxLimit = std::numeric_limits<T>::max() / 10;
T maxDigit = std::numeric_limits<T>::max() % 10;
try {
x = 0;
do {
// 检查是否会溢出
if (x > maxLimit || (x == maxLimit && (ch - '0') > maxDigit)) {
lastError = IOError::OutOfRange;
while ((ch = getChar()) >= '0' && ch <= '9'); // 跳过剩余数字
return *this;
}
x = x * 10 + (ch - '0'); // 构建数值
ch = getChar(); // 获取下一个字符
} while (ch >= '0' && ch <= '9');
// 如果不是 EOF,回退指针
if (ch != EOF) {
--current;
}
} catch (...) {
lastError = IOError::IOError;
}
return *this;
}
// 读取有符号整数
template < typename T, typename std::enable_if_t < std::is_signed_v<T>&& std::is_integral_v<T>, bool > = true >
inline __attribute__((always_inline)) istream & operator>>(T& x) noexcept {
clear(); // 清除错误状态
skipWhitespace<T>(); // 跳过空白字符
x = 0; // 初始化结果
char ch = getChar(); // 获取第一个非空白字符
if (ch == EOF) {
lastError = IOError::EndOfFile;
return *this;
}
bool negative = false; // 负数标记
// 处理负号
if (ch == '-') {
negative = true;
ch = getChar();
}
// 处理正号
else if (ch == '+') {
ch = getChar();
}
// 跳过前导零
while (ch == '0') {
ch = getChar();
if (ch == EOF) {
lastError = IOError::EndOfFile;
x = 0;
return *this;
}
}
// 检查是否为数字
if (ch < '0' || ch > '9') {
lastError = IOError::InvalidInput;
--current; // 回退指针
return *this;
}
using UnsignedT = typename std::make_unsigned_t<T>;
// 预计算最大值限制
UnsignedT maxLimit = std::numeric_limits<UnsignedT>::max() / 10;
UnsignedT maxDigit = std::numeric_limits<UnsignedT>::max() % 10;
try {
UnsignedT tmp = 0;
do {
// 检查是否会溢出
if (tmp > maxLimit || (tmp == maxLimit && (ch - '0') > maxDigit)) {
lastError = IOError::OutOfRange;
while ((ch = getChar()) >= '0' && ch <= '9'); // 跳过剩余数字
return *this;
}
tmp = tmp * 10 + (ch - '0'); // 构建数值
ch = getChar(); // 获取下一个字符
} while (ch >= '0' && ch <= '9');
// 如果不是EOF,回退指针
if (ch != EOF) {
--current;
}
// 处理符号和范围
if (negative) {
if (tmp > static_cast<UnsignedT>(std::numeric_limits<T>::max()) + 1) {
lastError = IOError::OutOfRange;
x = std::numeric_limits<T>::min();
} else {
x = -static_cast<T>(tmp);
}
} else {
if (tmp > static_cast<UnsignedT>(std::numeric_limits<T>::max())) {
lastError = IOError::OutOfRange;
x = std::numeric_limits<T>::max();
} else {
x = static_cast<T>(tmp);
}
}
} catch (...) {
lastError = IOError::IOError;
}
return *this;
}
// 读取浮点数
template <typename T, typename std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
inline __attribute__((always_inline)) istream & operator>>(T& x) noexcept {
clear(); // 清除错误状态
skipWhitespace<T>(); // 跳过空白字符
x = 0; // 初始化结果
char ch = getChar(); // 获取第一个非空白字符
if (ch == EOF) {
lastError = IOError::EndOfFile;
x = std::numeric_limits<T>::quiet_NaN();
return *this;
}
bool negative = false; // 负数标记
// 处理符号
if (ch == '-') {
negative = true;
ch = getChar();
} else if (ch == '+') {
ch = getChar();
}
T integerPart = 0; // 整数部分
bool hasDigits = false; // 是否有数字
// 读取整数部分
while (ch >= '0' && ch <= '9') {
hasDigits = true;
integerPart = integerPart * 10 + (ch - '0');
ch = getChar();
}
T fraction = 0; // 小数部分
T divisor = 1; // 小数位数除数
bool hasFraction = false; // 是否有小数部分
// 读取小数部分
if (ch == '.') {
ch = getChar();
while (ch >= '0' && ch <= '9') {
hasFraction = true;
fraction = fraction * 10 + (ch - '0');
divisor *= 10;
ch = getChar();
}
}
int exponent = 0; // 指数部分
// 读取指数部分
if (ch == 'e' || ch == 'E') {
ch = getChar();
bool expNegative = false;
// 处理指数符号
if (ch == '-') {
expNegative = true;
ch = getChar();
} else if (ch == '+') {
ch = getChar();
}
// 读取指数数字
while (ch >= '0' && ch <= '9') {
exponent = exponent * 10 + (ch - '0');
ch = getChar();
}
if (expNegative) {
exponent = -exponent;
}
}
// 如果没有数字,设置错误
if (!hasDigits && !hasFraction) {
lastError = IOError::InvalidInput;
--current;
return *this;
}
try {
// 计算最终值
x = (integerPart + (hasFraction ? fraction / divisor : 0)) * std::pow(10.0, exponent);
if (negative) x = -x;
} catch (...) {
lastError = IOError::IOError;
x = std::numeric_limits<T>::quiet_NaN();
}
return *this;
}
// 读取字符串
template <typename T, typename std::enable_if_t<std::is_same_v<T, std::string>, bool> = true>
inline __attribute__((always_inline)) istream & operator>>(T& str) noexcept {
clear(); // 清除错误状态
skipWhitespace<T>(); // 跳过空白字符
str.clear();
char ch;
// 读取非空白字符
while ((ch = getChar()) != EOF && !std::isspace(ch)) {
str.push_back(ch);
}
// 如果到达 EOF 且没有读取任何字符,设置错误
if (ch == EOF && str.empty()) {
lastError = IOError::EndOfFile;
}
return *this;
}
// 读取单个字符
inline __attribute__((always_inline)) istream& operator>>(char& ch) noexcept {
clear(); // 清除错误状态
skipWhitespace<char>(); // 跳过空白字符
ch = getChar();
// 如果到达 EOF 且没有读取任何字符,设置错误
if (ch == EOF) {
lastError = IOError::EndOfFile;
}
return *this;
}
// 读取 C 风格字符串
inline __attribute__((always_inline)) istream& operator>>(char* str) noexcept {
clear(); // 清除错误状态
skipWhitespace<char>(); // 跳过空白字符
char ch;
size_t i = 0;
// 读取非空白字符
while ((ch = getChar()) != EOF && !(ch == ' ' || ch == '\n' || ch == '\t' || ch == '\r' || ch == '\v' || ch == '\f')) {
if (i < std::numeric_limits<size_t>::max() - 1) {
str[i++] = ch;
}
}
// 如果到达 EOF 且没有读取任何字符,设置错误
if (ch == EOF && i == 0) {
lastError = IOError::EndOfFile;
}
str[i] = '\0'; // 添加字符串结束符
return *this;
}
// 读取布尔值
inline __attribute__((always_inline)) istream& operator>>(bool& b) noexcept {
clear(); // 清除错误状态
skipWhitespace<int>(); // 跳过空白字符
b = false;
char ch = getChar();
if (ch == EOF) {
lastError = IOError::EndOfFile;
return *this;
}
// 跳过符号
if (ch == '-' || ch == '+') ch = getChar();
// 跳过前导零
while (ch == '0') {
ch = getChar();
if (ch == EOF) {
lastError = IOError::EndOfFile;
b = 0;
return *this;
}
}
// 检查是否为数字
if (ch < '0' || ch > '9') {
lastError = IOError::InvalidInput;
--current;
return *this;
}
// 读取数字,只要不是 0 就设为 true
do {
if (ch != '0') {
b = true;
return *this;
}
ch = getChar();
} while (ch >= '0' && ch <= '9');
if (ch != EOF) {
--current;
}
return *this;
}
};
// 输出流类
class ostream {
private:
char* buffer; // 输出缓冲区
char* current; // 当前写入位置
char* end; // 缓冲区结束位置
const size_t bufferSize; // 缓冲区大小
// 格式化选项
unsigned int Base = 10; // 数值进制
unsigned int Precision = 6; // 浮点数精度
unsigned int Width = 0; // 字段宽度
char Fill = ' '; // 填充字符
bool Align = false; // 对齐方式
bool ShowPos = false; // 显示正号
bool ShowBase = false; // 显示基数前缀
bool Case = false; // 大写字母
bool Scientific = false; // 科学计数法
bool Fixed = false; // 固定小数点
bool BoolAlpha = false; // 布尔值字母表示
bool ShowPoint = true; // 显示小数点
// 刷新缓冲区
constexpr inline __attribute__((always_inline)) void flushBuffer() noexcept {
if (current != buffer) {
// 将缓冲区内容写入标准输出
fwrite(buffer, 1, current - buffer, stdout);
current = buffer; // 重置当前位置
}
}
// 写入一个字符
constexpr inline __attribute__((always_inline)) void putChar(char ch) noexcept {
// 如果缓冲区满,先刷新
if (current == end) {
flushBuffer();
}
*current++ = ch; // 写入字符并移动指针
}
// 输出整数
template <typename T, typename std::enable_if_t<std::is_integral_v<T>, bool> = true>
inline __attribute__((always_inline)) void outputInteger(T x) noexcept {
char numBuf[MAX_NUMERIC_BUFFER_SIZE]; // 数值转换缓冲区
char* p = numBuf + MAX_NUMERIC_BUFFER_SIZE - 1; // 指向缓冲区末尾
*p = '\0'; // 字符串结束符
// 根据进制选择数字字符
const char* digits = Case ? "0123456789ABCDEF" : "0123456789abcdef";
// 处理负数
bool isNegative = std::is_signed_v<T>&& x < 0;
using UnsignedT = typename std::make_unsigned_t<T>;
UnsignedT ux = isNegative ? -static_cast<UnsignedT>(x) : static_cast<UnsignedT>(x);
// 转换数字为字符串
if (ux == 0) {
*--p = '0';
} else {
while (ux > 0) {
*--p = digits[ux % Base];
ux /= Base;
}
}
// 添加基数前缀
if (ShowBase) {
switch (Base) {
case 16:
*--p = Case ? 'X' : 'x';
*--p = '0';
break;
case 8:
*--p = Case ? 'O' : 'o';
*--p = '0';
break;
case 2:
*--p = Case ? 'B' : 'b';
*--p = '0';
break;
}
}
// 添加符号
if (isNegative) {
*--p = '-';
} else if (ShowPos) {
*--p = '+';
}
// 计算数字长度
size_t numLen = (numBuf + MAX_NUMERIC_BUFFER_SIZE - 1) - p;
// 处理字段宽度和对齐
if (Width > numLen) {
size_t padding = Width - numLen;
if (!Align) { // 右对齐
for (size_t i = 0; i < padding; ++i) putChar(Fill);
}
// 输出数字
while (*p) putChar(*p++);
if (Align) { // 左对齐
for (size_t i = 0; i < padding; ++i) putChar(Fill);
}
} else {
// 直接输出数字
while (*p) putChar(*p++);
}
}
// 输出浮点数
template <typename T, typename std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
inline __attribute__((always_inline)) void outputFloating(T x) noexcept {
// 处理特殊值
if (std::isnan(x)) {
outputString(Case ? "NAN" : "nan");
return;
}
if (std::isinf(x)) {
if (x < 0) putChar('-');
else if (ShowPos) putChar('+');
outputString(Case ? "INF" : "inf");
return;
}
// 处理符号
if (x < 0) {
putChar('-');
x = -x;
} else if (ShowPos) {
putChar('+');
}
// 添加舍入因子
long double roundingFactor = 0.5000001l * std::pow(10.0l, -static_cast<int>(Precision));
x += roundingFactor;
// 科学计数法
if (Scientific) {
int exponent = 0;
if (x != 0) {
exponent = static_cast<int>(std::log10(x));
x /= std::pow(10.0, exponent);
}
// 输出整数部分
putChar('0' + static_cast<int>(x));
x -= static_cast<int>(x);
// 输出小数部分
if (Precision > 0) {
if (ShowPoint) putChar('.');
for (unsigned int i = 0; i < Precision; ++i) {
x *= 10;
putChar('0' + static_cast<int>(x));
x -= static_cast<int>(x);
}
}
// 输出指数部分
putChar(Case ? 'E' : 'e');
if (exponent < 0) {
putChar('-');
exponent = -exponent;
} else {
putChar('+');
}
if (exponent < 10) putChar('0');
outputInteger(exponent);
}
// 固定小数点
else if (Fixed) {
T integerPart = std::floor(x);
T fractionalPart = x - integerPart;
// 输出整数部分
outputInteger(static_cast<long long>(integerPart));
// 输出小数部分
if (Precision > 0) {
if (ShowPoint) putChar('.');
for (unsigned int i = 0; i < Precision; ++i) {
fractionalPart *= 10;
int digit = static_cast<int>(fractionalPart);
putChar('0' + digit);
fractionalPart -= digit;
}
}
}
// 默认浮点表示
else {
if (x == 0) {
putChar('0');
if (Precision > 0) {
if (ShowPoint) putChar('.');
for (unsigned int i = 0; i < Precision; ++i) putChar('0');
}
return;
}
// 计算指数
int exponent = static_cast<int>(std::log10(x));
// 使用科学计数法如果指数太小或太大
if (exponent < -4 || (exponent > Precision && x != 0)) {
x /= std::pow(10.0, exponent);
putChar('0' + static_cast<int>(x));
x -= static_cast<int>(x);
if (Precision > 0) {
if (ShowPoint) putChar('.');
for (unsigned int i = 0; i < Precision; ++i) {
x *= 10;
putChar('0' + static_cast<int>(x));
x -= static_cast<int>(x);
}
}
putChar(Case ? 'E' : 'e');
if (exponent < 0) {
putChar('-');
exponent = -exponent;
} else {
putChar('+');
}
if (exponent < 10) putChar('0');
outputInteger(exponent);
} else {
// 处理小指数
if (exponent < 0) {
putChar('0');
if (ShowPoint) putChar('.');
for (int i = -1; i > exponent; --i) putChar('0');
T scaled = x * std::pow(10.0, -exponent - 1);
for (unsigned int i = 0; i < static_cast<unsigned int>(-exponent - 1) + Precision; ++i) {
scaled *= 10;
putChar('0' + static_cast<int>(scaled));
scaled -= static_cast<int>(scaled);
}
} else {
// 正常输出
T integerPart = std::floor(x);
T fractionalPart = x - integerPart;
outputInteger(static_cast<long long>(integerPart));
if (Precision > 0) {
if (ShowPoint) putChar('.');
for (unsigned int i = 0; i < Precision; ++i) {
fractionalPart *= 10;
int digit = static_cast<int>(fractionalPart);
putChar('0' + digit);
fractionalPart -= digit;
}
}
}
}
}
}
// 输出字符串(处理格式化)
inline __attribute__((always_inline)) void outputString(const char* str) noexcept {
size_t len = strlen(str);
// 处理字段宽度和对齐
if (Width > len) {
size_t padding = Width - len;
if (!Align) { // 右对齐
for (size_t i = 0; i < padding; ++i) putChar(Fill);
}
// 输出字符串
while (*str) putChar(*str++);
if (Align) { // 左对齐
for (size_t i = 0; i < padding; ++i) putChar(Fill);
}
} else {
// 直接输出字符串
while (*str) putChar(*str++);
}
}
public:
// 构造函数,分配缓冲区
explicit ostream(size_t bufSize = DEFAULT_BUFFER_SIZE)
: buffer(new char[bufSize]), current(buffer), end(buffer + bufSize), bufferSize(bufSize) {}
// 析构函数,刷新缓冲区并释放内存
~ostream() noexcept {
flushBuffer();
delete[] buffer;
}
// 强制刷新缓冲区
constexpr inline __attribute__((always_inline)) void flush() noexcept {
flushBuffer();
fflush(stdout);
}
// 设置进制
inline __attribute__((always_inline)) ostream& setbase(unsigned int b) noexcept {
Base = b;
return *this;
}
// 设置精度
inline __attribute__((always_inline)) ostream& setprecision(unsigned int p) noexcept {
Precision = p;
return *this;
}
// 设置字段宽度
inline __attribute__((always_inline)) ostream& setw(unsigned int w) noexcept {
Width = w;
return *this;
}
// 设置填充字符
inline __attribute__((always_inline)) ostream& setfill(char c) noexcept {
Fill = c;
return *this;
}
// 左对齐
inline __attribute__((always_inline)) ostream& left() noexcept {
Align = true;
return *this;
}
// 右对齐
inline __attribute__((always_inline)) ostream& right() noexcept {
Align = false;
return *this;
}
// 显示正号
inline __attribute__((always_inline)) ostream& showpos() noexcept {
ShowPos = true;
return *this;
}
// 不显示正号
inline __attribute__((always_inline)) ostream& noshowpos() noexcept {
ShowPos = false;
return *this;
}
// 显示基数前缀
inline __attribute__((always_inline)) ostream& showbase() noexcept {
ShowBase = true;
return *this;
}
// 不显示基数前缀
inline __attribute__((always_inline)) ostream& noshowbase() noexcept {
ShowBase = false;
return *this;
}
// 使用大写字母
inline __attribute__((always_inline)) ostream& uppercase() noexcept {
Case = true;
return *this;
}
// 不使用大写字母
inline __attribute__((always_inline)) ostream& nouppercase() noexcept {
Case = false;
return *this;
}
// 科学计数法
inline __attribute__((always_inline)) ostream& scientific() noexcept {
Scientific = true;
Fixed = false;
return *this;
}
// 固定小数点
inline __attribute__((always_inline)) ostream& fixed() noexcept {
Fixed = true;
Scientific = false;
return *this;
}
// 默认浮点表示
inline __attribute__((always_inline)) ostream& defaultfloat() noexcept {
Scientific = false;
Fixed = false;
return *this;
}
// 布尔值字母表示
inline __attribute__((always_inline)) ostream& boolalpha() noexcept {
BoolAlpha = true;
return *this;
}
// 布尔值数字表示
inline __attribute__((always_inline)) ostream& noboolalpha() noexcept {
BoolAlpha = false;
return *this;
}
// 显示小数点
inline __attribute__((always_inline)) ostream& showpoint() noexcept {
ShowPoint = true;
return *this;
}
// 不显示小数点
inline __attribute__((always_inline)) ostream& noshowpoint() noexcept {
ShowPoint = false;
return *this;
}
// 输出整数
template <typename T, typename std::enable_if_t<std::is_integral_v<T>, bool> = true>
inline __attribute__((always_inline)) ostream & operator<<(T x) noexcept {
outputInteger(x);
return *this;
}
// 输出浮点数
template <typename T, typename std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
inline __attribute__((always_inline)) ostream & operator<<(T x) noexcept {
outputFloating(x);
return *this;
}
// 输出字符串
inline __attribute__((always_inline)) ostream& operator<<(const std::string& str) noexcept {
outputString(str.c_str());
return *this;
}
// 输出单个字符
inline __attribute__((always_inline)) ostream& operator<<(char ch) noexcept {
putChar(ch);
return *this;
}
// 输出 C 风格字符串
inline __attribute__((always_inline)) ostream& operator<<(const char* str) noexcept {
outputString(str);
return *this;
}
// 输出布尔值
inline __attribute__((always_inline)) ostream& operator<<(bool b) noexcept {
if (BoolAlpha) {
outputString(b ? (Case ? "TRUE" : "true") : (Case ? "FALSE" : "false"));
} else {
putChar(b ? '1' : '0');
}
return *this;
}
// 输出指针
inline __attribute__((always_inline)) ostream& operator<<(const void* p) noexcept {
// 临时修改格式设置输出指针
unsigned int oldBase = Base;
bool oldShowBase = ShowBase;
Base = 16;
ShowBase = true;
outputInteger(reinterpret_cast<uintptr_t>(p));
Base = oldBase;
ShowBase = oldShowBase;
return *this;
}
// 处理操纵器
inline __attribute__((always_inline)) ostream& operator<<(ostream & (*manip)(ostream&)) noexcept {
return manip(*this);
}
};
// 操纵器实现
inline __attribute__((always_inline)) ostream& flush(ostream& os) noexcept {
os.flush();
return os;
}
inline __attribute__((always_inline)) ostream& left(ostream& os) noexcept {
os.left();
return os;
}
inline __attribute__((always_inline)) ostream& right(ostream& os) noexcept {
os.right();
return os;
}
inline __attribute__((always_inline)) ostream& showpos(ostream& os) noexcept {
os.showpos();
return os;
}
inline __attribute__((always_inline)) ostream& noshowpos(ostream& os) noexcept {
os.noshowpos();
return os;
}
inline __attribute__((always_inline)) ostream& showbase(ostream& os) noexcept {
os.showbase();
return os;
}
inline __attribute__((always_inline)) ostream& noshowbase(ostream& os) noexcept {
os.noshowbase();
return os;
}
inline __attribute__((always_inline)) ostream& uppercase(ostream& os) noexcept {
os.uppercase();
return os;
}
inline __attribute__((always_inline)) ostream& nouppercase(ostream& os) noexcept {
os.nouppercase();
return os;
}
inline __attribute__((always_inline)) ostream& scientific(ostream& os) noexcept {
os.scientific();
return os;
}
inline __attribute__((always_inline)) ostream& fixed(ostream& os) noexcept {
os.fixed();
return os;
}
inline __attribute__((always_inline)) ostream& defaultfloat(ostream& os) noexcept {
os.defaultfloat();
return os;
}
inline __attribute__((always_inline)) ostream& boolalpha(ostream& os) noexcept {
os.boolalpha();
return os;
}
inline __attribute__((always_inline)) ostream& noboolalpha(ostream& os) noexcept {
os.noboolalpha();
return os;
}
inline __attribute__((always_inline)) ostream& showpoint(ostream& os) noexcept {
os.showpoint();
return os;
}
inline __attribute__((always_inline)) ostream& noshowpoint(ostream& os) noexcept {
os.noshowpoint();
return os;
}
// 格式化操纵器类
class setbase {
unsigned int base;
public:
explicit setbase(unsigned int p) noexcept : base(p) {}
friend ostream& operator<<(ostream& os, const setbase& manip) noexcept {
return os.setbase(manip.base);
}
};
class setprecision {
unsigned int precision;
public:
explicit setprecision(unsigned int p) noexcept : precision(p) {}
friend ostream& operator<<(ostream& os, const setprecision& manip) noexcept {
return os.setprecision(manip.precision);
}
};
class setw {
unsigned int width;
public:
explicit setw(unsigned int w) noexcept : width(w) {}
friend ostream& operator<<(ostream& os, const setw& manip) noexcept {
return os.setw(manip.width);
}
};
class setfill {
char fill;
public:
explicit setfill(char c) noexcept : fill(c) {}
friend ostream& operator<<(ostream& os, const setfill& manip) noexcept {
return os.setfill(manip.fill);
}
};
class hex {
public:
friend ostream& operator<<(ostream& os, const hex&) noexcept {
return os.setbase(16).showbase();
}
};
class oct {
public:
friend ostream& operator<<(ostream& os, const oct&) noexcept {
return os.setbase(8).showbase();
}
};
class bin {
public:
friend ostream& operator<<(ostream& os, const bin&) noexcept {
return os.setbase(2).showbase();
}
};
class dec {
public:
friend ostream& operator<<(ostream& os, const dec&) noexcept {
return os.setbase(10).noshowbase();
}
};
// 换行并刷新
inline __attribute__((always_inline)) ostream& endl(ostream& os) noexcept {
os << '\n';
os.flush();
return os;
}
// 字符串结束符
inline __attribute__((always_inline)) ostream& ends(ostream& os) noexcept {
os << '\0';
return os;
}
// 全局流对象
inline istream cin;
inline ostream cout;
}剪贴板
剪贴板 66yoybz9
剪贴板信息
剪贴板信息
- 收录
- 更新