std::bitset::to_string(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::bitset::to_string(3) πŸ’…

okay so bestie, this page is straight outta the C++ Standard Libary manual, and it’s giving β€œturn my bits into a string” energy. no cap, it’s a std::bitset member function, and it’s lowkey iconic.

✨ NAME ✨

std::bitset::to_string β€” std::bitset::to_string (yes, that’s the name, she said what she said).

πŸ¦‹ SYNOPSIS πŸ¦‹

          template< class CharT, class Traits, class Allocator
          >

          std::basic_string<CharT, Traits, Allocator>                  (until
       C++11)
              to_string( CharT zero = CharT('0'),

                         CharT one = CharT('1') ) const;
          template<

              class CharT = char,
              class Traits = std::char_traits<CharT>,
              class Allocator = std::allocator<CharT>                  (since
       C++11)
          >
       (constexpr since C++23)
          std::basic_string<CharT, Traits, Allocator>
              to_string( CharT zero = CharT('0'),
                                                               (1)
                         CharT one = CharT('1') ) const;
          template< class CharT, class Traits >

          std::basic_string<CharT, Traits>                         (2) (until
       C++11)
              to_string( CharT zero = CharT('0'),

                         CharT one = CharT('1') ) const;
          template< class CharT >

          std::basic_string<CharT> to_string( CharT zero =
          CharT('0'),                                              (3) (until
       C++11)

                                              CharT one =
          CharT('1') ) const;
          std::string to_string( char zero = '0', char one =       (4) (until
       C++11)
          '1' ) const;

Converts the contents of the bitset to a string. Uses zero to represent bits with value of false and one to represent bits with value of true.

The resulting string contains N characters: the first character corresponds to the last (N-1^th) bit, and the last character corresponds 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 to_string (all of these helper overloads are (until C++11)):

  • 2) Uses the default allocator std::allocator.
  • 3) Uses the default character trait std::char_traits and the default allocator std::allocator.
  • 4) Uses the default character type char, the default character trait std::char_traits and the default allocator std::allocator.

πŸŽ€ PARAMETERS πŸŽ€

  • zero β€” character to use to represent false.
  • one β€” character to use to represent true.

🌈 RETURN VALUE 🌈

  1. The converted string.
  2. to_string<CharT, Traits, std::allocator<CharT>>(zero, one).
  3. to_string<CharT, std::char_traits<CharT>, std::allocator<CharT>>(zero, one).
  4. to_string<char, std::char_traits<char>, std::allocator<char>>(zero, one).

😭 EXCEPTIONS 😭

May throw std::bad_alloc from the std::basic_string constructor. so yeah, allocation says no sometimes πŸ’€

πŸ’œ NOTES πŸ’œ

Since C++11, function templates can have default template arguments. LWG issue 1113 removed the helper overloads (2-4) and added the corresponding default template arguments in (1).

🌸 EXAMPLE 🌸

       // Run this code

        #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

πŸ’€ DEFECT REPORTS πŸ’€

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. dramatic but true, bestie 🫢

  • LWG 396 β€” applied to C++98. Behavior as published: zero and one bits were converted to characters 0 and 1 (which do not correspond to '0' and '1'). Correct behavior: added parameters to provide values for these characters.
  • LWG 434 β€” applied to C++98. Behavior as published: all template arguments needed to be provided. Correct behavior: added overloads (2-4).
  • LWG 853 β€” applied to C++98. Behavior as published: overloads (2-4) did not have the default arguments added by LWG issue 396. Correct behavior: also added.

πŸ‘€ SEE ALSO πŸ‘€

  • to_ulong β€” returns an unsigned long integer representation of the data (public member function).
  • to_ullong β€” returns an unsigned long long integer representation of the data (C++11) (public member function).

k, that’s the whole page! source/version vibe: http://cppreference.com, dated 2024.06.10 β€” mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from