Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::bitset<N>::to_string

      From cppreference.com
      <cpp‎ |utility‎ |bitset
       
       
      Utilities library
       
       
      (1)
      template<class CharT,class Traits,class Allocator>

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

                     CharT one= CharT('1'))const;
      (until C++11)
      template<

         class CharT=char,
         class Traits=std::char_traits<CharT>,
         class Allocator=std::allocator<CharT>
      >
      std::basic_string<CharT, Traits, Allocator>
          to_string( CharT zero= CharT('0'),

                     CharT one= CharT('1'))const;
      (since C++11)
      (constexpr since C++23)
      template<class CharT,class Traits>

      std::basic_string<CharT, Traits>
          to_string( CharT zero= CharT('0'),

                     CharT one= CharT('1'))const;
      (2)(until C++11)
      template<class CharT>

      std::basic_string<CharT> to_string( CharT zero= CharT('0'),

                                          CharT one= CharT('1'))const;
      (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 ofto_string:

      2) Uses the default allocatorstd::allocator.
      3) Uses the default character traitstd::char_traits and 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

      [edit]Parameters

      zero - character to use to representfalse
      one - character to use to representtrue

      [edit]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).

      [edit]Exceptions

      May throwstd::bad_alloc from thestd::basic_string constructor.

      [edit]Notes

      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).

      [edit]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

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 396C++98zero 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 434C++98all template arguments needed to be providedadded overloads(2-4)
      LWG 853C++98overloads(2-4) did not have the default
      arguments added byLWG issue 396
      also added

      [edit]See also

      returns anunsignedlong integer representation of the data
      (public member function)[edit]
      (C++11)
      returns anunsignedlonglong integer representation of the data
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/bitset/to_string&oldid=172311"

      [8]ページ先頭

      ©2009-2025 Movatter.jp