{
}
Online Python Compiler
Online R Compiler
Online SQL Editor
Online HTML/CSS Editor
Online Java Compiler
Online C Compiler
Online C++ Compiler
Online C# Compiler
Online JavaScript Compiler
Online Typescript Compiler
Typescript Online Compiler
Online GoLang Compiler
Online Rust Compiler
Scala Online Compiler
Dart Online Compiler
Ruby Online Compiler
Online PHP Compiler
Online Swift Compiler
Generating Link
Generating Link
Share your code
Share code
Copy Link
Copied to clipboard
or share using
CYBER
CYBER
CYBER
CYBER
CYBER
66
%
OFF
Week
Week
Week
Week
Week
Stop copy pasting code you don't actually understand
Build the coding confidence you need to become a developer companies will fight for
Stop copy pasting code you don't actually understand
Become a PRO
Become a PRO
CYBER
CYBER
CYBER
CYBER
CYBER
66
%
OFF
Week
Week
Week
Week
Week
Stop copy pasting code you don't actually understand
Build the coding confidence you need to become a developer companies will fight for
Stop copy pasting code you don't actually understand
Become a PRO
Become a PRO
C++ Online Compiler
Learn Python App
Learn Python
main.cpp
Output
main.cpp
Share
Run
Run
#include <iostream> #include <iomanip> // 尽管这里主要用flags,但对比时会用到 int main() { int num = 255; double pi = 3.14; std::cout << "--- 默认输出 ---" << std::endl; std::cout << num << std::endl; std::cout << pi << std::endl; std::cout << true << std::endl; std::cout << "\n--- 使用 Formatting Flags ---" << std::endl; // 1. 保存当前状态 std::ios_base::fmtflags original_flags = std::cout.flags(); // 2. 设置新标志 // 设置为十六进制,大写,显示进制前缀 std::cout.setf(std::ios::hex | std::ios::uppercase | std::ios::showbase); std::cout << "设置 hex, uppercase, showbase: " << num << std::endl; // 输出: 0XFF // 清除 uppercase 标志 std::cout.unsetf(std::ios::uppercase); std::cout << "清除 uppercase: " << num << std::endl; // 输出: 0xff // 使用 setf 的第二种形式来改变进制(这是一个互斥组) // 先清除 basefield 组的所有标志,再设置 dec 标志 std::cout.setf(std::ios::dec, std::ios::basefield); std::cout << "切换回 dec: " << num << std::endl; // 输出: 255 // 设置 boolalpha std::cout.setf(std::ios::boolalpha); std::cout << "设置 boolalpha: " << true << std::endl; // 输出: true // 3. 恢复原始状态 std::cout.flags(original_flags); std::cout << "\n--- 恢复默认输出 ---" << std::endl; std::cout << num << std::endl; std::cout << true << std::endl; return 0; }
Output
Clear