std::setbase
Defined in header <iomanip> | | |
|---|---|---|
/*unspecified*/ setbase( int base ); | | |
设置流的数字基。在表达式中使用时out << setbase(base)或in >> setbase(base),更改basefield小溪旗out或in,取决于base*
- 价值
16集basefield到std::ios_base::hex
- 价值
8集std::ios_base::oct
- 价值
10集std::ios_base::dec...
值...的值base除8、10或16重置外basefield为零,这对应于十进制输出和前缀相关的输入。
参数
base | - | new value for basefield |
|---|
返回值
返回未指定类型的对象,以便在str类型的输出流的名称。std::basic_ostream<CharT, Traits>或std::basic_istream<CharT, Traits>,然后表达str << setbase(base)或str >> setbase(base)行为就像执行了以下代码:
二次
str.setf(base == 8 ? std::ios_base::oct :
base == 10 ? std::ios_base::dec :
base == 16 ? std::ios_base::hex :
std::ios_base::fmtflags(0),
std::ios_base::basefield);二次
例
二次
#include <iostream>
#include <sstream>
#include <iomanip>
int main()
{
std::cout << "Parsing string \"10 0x10 010\"\n";
int n1, n2, n3;
std::istringstream s("10 0x10 010");
s >> std::setbase(16) >> n1 >> n2 >> n3;
std::cout << "hexadecimal parse: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
s.clear();
s.seekg(0);
s >> std::setbase(0) >> n1 >> n2 >> n3;
std::cout << "prefix-dependent parse: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
std::cout << "hex output: " << std::setbase(16)
<< std::showbase << n1 << ' ' << n2 << ' ' << n3 << '\n';
}二次
产出:
二次
Parsing string "10 0x10 010"
hexadecimal parse: 16 16 16
prefix-dependent parse: 10 16 8
hex output: 0xa 0x10 0x8二次
另见
dechexoct | changes the base used for integer I/O (function) |
|---|---|
showbasenoshowbase | controls whether prefix is used to indicate numeric base (function) |
© cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

