|
|
Member types | ||||
Member functions | ||||
(until C++20) | ||||
Element access | ||||
Capacity | ||||
Modifiers | ||||
Conversions | ||||
bitset::to_string | ||||
(C++11) | ||||
Non-member functions | ||||
Helper classes | ||||
(C++11) |
(1) | ||
template<class CharT,class Traits,class Allocator> std::basic_string<CharT, Traits, Allocator> | (until C++11) | |
template< class CharT=char, | (since C++11) (constexpr since C++23) | |
template<class CharT,class Traits> std::basic_string<CharT, Traits> | (2) | (until C++11) |
template<class CharT> std::basic_string<CharT> to_string( CharT zero= CharT('0'), | (3) | (until C++11) |
std::string to_string(char zero='0',char one='1')const; | (4) | (until C++11) |
Converts the contents of the bitset to a string. Useszero to represent bits with value offalse andone to represent bits with value oftrue.
The resulting string containsN characters with the first character corresponds to the last (N-1th) bit and the last character corresponding to the first bit.
All template type arguments need to be provided because function templates cannot have default template arguments. Overloads(2-4) are provided to simplify the invocations of 2) Uses the default allocatorstd::allocator. 4) Uses the default character typechar, the default character traitstd::char_traits and the default allocatorstd::allocator. | (until C++11) |
Contents |
zero | - | character to use to representfalse |
one | - | character to use to representtrue |
May throwstd::bad_alloc from thestd::basic_string constructor.
Since C++11, functions templates can have default template arguments.LWG issue 1113 removed the helper overloads(2-4) and added the corresponding default template arguments in(1).
#include <bitset>#include <iostream> int main(){std::bitset<8> b{42};std::cout<< b.to_string()<<'\n'<< b.to_string('*')<<'\n'<< b.to_string('O','X')<<'\n';}
Output:
00101010**1*1*1*OOXOXOXO
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 396 | C++98 | zero and one bits were converted to characters0 and1 (which do not correspond to'0' and'1') | added parameters to provide values for these characters |
LWG 434 | C++98 | all template arguments needed to be provided | added overloads(2-4) |
LWG 853 | C++98 | overloads(2-4) did not have the default arguments added byLWG issue 396 | also added |
returns anunsignedlong integer representation of the data (public member function)[edit] | |
(C++11) | returns anunsignedlonglong integer representation of the data (public member function)[edit] |