C++ Bitset 库 - to_string() 函数



描述

C++ 函数std::bitset::test()测试第 N 位是否被设置。

描述

C++ 函数std::bitset::to_string()将 bitset 对象转换为字符串对象。

声明

以下是来自 std::bitset 头文件的 std::bitset::to_string() 函数声明。

C++98

template <class charT, class traits, class Alloc>
basic_string<charT,traits,Alloc> to_string() const;

C++11

template <class charT = char,
          class traits = char_traits<charT>,
          class Alloc = allocator<charT>>
          basic_string<charT,traits,Alloc> to_string (charT zero = charT('0'),
          charT one  = charT('1')) const;

参数

返回值

返回 bitset 对象的字符串表示。

异常

如果抛出异常,bitset 不发生变化。

示例

以下示例演示了 std::bitset::to_string() 函数的使用。

#include <iostream>
#include <bitset>

using namespace std;

int main(void) {

   bitset<4> b;

   string s = b.to_string();

   cout << s << endl;

   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果:

0000
bitset.htm
广告
© . All rights reserved.