首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::setbase

Defined in header <iomanip>

?

?

/*unspecified*/ setbase( int base );

?

?

设置流的数字基。在表达式中使用时out << setbase(base)in >> setbase(base),更改basefield小溪旗outin,取决于base*

  • 价值16basefieldstd::ios_base::hex
  • 价值8std::ios_base::oct
  • 价值10std::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)行为就像执行了以下代码:

二次

代码语言:javascript
复制
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);

二次

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
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)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com