cout << "hex:" << hex << 11 << " " << 12 << " " << 13 << endl << "oct:" << oct << 11 << " " << 12 << " " << 13 << endl << "dec:" << dec << 11 << " " << 12 << " " << 13 << endl;
hex 以 16 进制输出流后面的整数,oct 以 8 进制输出流后面的整数,dec 恢复回十进制。 hex、oct 这种改变流的行为的关键字被称为操纵符。
hex、oct 这种一旦改变输出格式、后面的数字将一直以这种进制输出(持久):
cout << hex << 10 << endl; // 输出 a cout << 10 << endl; // 还是输出 a
showbase 显示进制前缀,输出十六进制数时添加前缀 0x,输出八进制数时添加前缀 0:
cout << showbase << hex << 11 << endl; // 0xb cout << oct << 11 << endl; // 013
unshowbase 取消前缀:
cout << showbase << hex; cout << 11 << endl; // 0xb cout << unshowbase; cout << 11; // b
hex oct dec 也可以用在 cin 上:
int a, b, c; cin >> a >> hex >> b >> oct >> c; cout << a << " " << b << " " << c;
输入和输出:
输入: 10 0xa 012 输出: 10 10 10 输入: 10 a 12 输出: 10 10 10
三种输出格式:
double num = 1234567890.123456789; cout << num << "\n" // defaultfloat: 1.23457e+09 << fixed << num << "\n" // fixed: 1234567890.123457 << scientific << num << "\n"; // scientific: 1.234568e+09
显示小数点后 n 位:fixed + setprecision(),需要引入 iomanip 库
#include <iomanip> double num = 1234567890.123456789; cout << setprecision(12) << num << "\n" // 1234567890.12 << fixed << num << "\n" // 1234567890.123456716537 << scientific << num << "\n"; // 1.234567890123e+09
set(),非持久,只对下一次的输出有效:
const int w = 16; cout << "|" << setw(w) << "Hello" << "|\n"; // | Hello| cout << "|" << setw(w) << "123" << "|\n"; // | 123| cout << "|" << setw(w) << "abc" << "|\n"; // | abc|
需要引入头文件:
#include <sstream>
字符串转数字:
istringstream is { s }; double d; is >> d; if (!is) throw runtime_error("double format error: " + s);
数字转字符串:
ostringstream os; int x = 12, y = 34; os << "Point { " << x << ", " << y << " }"; string s = os.str();
使用 getline() 函数,返回的字符串不包含回车符号。
string l; getline(cin, l);