public member function
<bitset>

std::bitset::to_string

template <class charT, class traits, class Alloc>  basic_string<charT,traits,Alloc> to_string() const;
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;
Convert to string
Constructs a basic_string object that represents the bits in the bitset as a succession of zeros and/or ones.

The string returned by this function has the same representation as the output produced by inserting the bitset directly into an output stream with operator<<.

Notice that this function template uses the template parameters to define the return type. Therefore, these are not implicitly deduced.

Parameters

none
Constructs a basic_string object that represents the bits in the bitset as a succession of zeros and/or ones.

The string returned by this function has the same representation as the output produced by inserting the bitset directly into an output stream with operator<<.

Notice that this function template uses the template parameters to define the return type. The default types for these template parameters select string as the return type.

Parameters

zero, one
Character values to represent zero and one.

Return value

A string representing the bits in the bitset.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// bitset::to_string
#include <iostream>       // std::cout
#include <string>         // std::string
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<4> mybits;     // mybits: 0000
  mybits.set();              // mybits: 1111

  std::string mystring =
    mybits.to_string<char,std::string::traits_type,std::string::allocator_type>();

  std::cout << "mystring: " << mystring << '\n';

  return 0;
}

Output:

mystring: 1111


Data races

The bitset is accessed.

Exception safety

Strong guarantee: if an exception is thrown, there are no changes in the bitset.

See also