Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::bitset<N>::bitset

      From cppreference.com
      <cpp‎ |utility‎ |bitset
       
       
      Utilities library
       
       
      bitset();
      (1)(noexcept since C++11)
      (constexpr since C++11)
      (2)
      bitset(unsignedlong val);
      (until C++11)
      constexpr bitset(unsignedlonglong val)noexcept;
      (since C++11)
      template<class CharT,class Traits,class Alloc>

      explicit bitset
         (conststd::basic_string<CharT, Traits, Alloc>& str,
           typenamestd::basic_string
                         <CharT, Traits, Alloc>::size_type pos=0,
           typenamestd::basic_string<CharT, Traits, Alloc>::size_type
                n=std::basic_string<CharT, Traits, Alloc>::npos,

            CharT zero= CharT('0'), CharT one= CharT('1'));
      (3)(constexpr since C++23)
      template<class CharT,class Traits>

      constexprexplicit bitset
         (std::basic_string_view<CharT, Traits> str,
           std::size_t pos=0,std::size_t n=std::size_t(-1),

            CharT zero= CharT('0'), CharT one= CharT('1'));
      (4)(since C++26)
      template<class CharT>

      explicit bitset(const CharT* str,std::size_t n=std::size_t(-1),

                       CharT zero= CharT('0'), CharT one= CharT('1'));
      (5)(since C++11)
      (constexpr since C++23)

      Constructs a new bitset from one of several optional data sources:

      1) Default constructor. Constructs a bitset with all bits set to zero.
      2) Constructs a bitset from an unsigned integerval.
      Given the number of bits in thevalue representation ofunsignedlong(until C++11)unsignedlonglong(since C++11) asS:
      • The first (rightmost, least significant)std::min(S, N) bit positions are initialized with the corresponding bit values ofval.
      • IfS is less thanN, the remaining bit positions are initialized to zeroes.
      3) Constructs a bitset using the characters instr. An optional starting positionpos and lengthn can be provided, as well as characters denoting alternate values for set (one) and unset (zero) bits.Traits::eq() is used to compare the character values.
      The effective length of the initializing string isstd::min(n, str.size()- pos).
      4) Similar to(3), but uses astd::basic_string_view instead of astd::basic_string.
      5) Similar to(3), but uses aconst CharT* instead of astd::basic_string.

      Equivalent tobitset(n==std::basic_string<CharT>::npos
               ?std::basic_string<CharT>(str)
               :std::basic_string<CharT>(str, n),0, n, zero, one)
      .

      (until C++26)

      Equivalent tobitset(n==std::basic_string_view<CharT>::npos
               ?std::basic_string_view<CharT>(str)
               :std::basic_string_view<CharT>(str, n),0, n, zero, one)
      .

      (since C++26)

      Contents

      [edit]Parameters

      val - number used to initialize the bitset
      str - string used to initialize the bitset
      pos - a starting offset intostr
      n - number of characters to use fromstr
      zero - alternate character for unset bits instr
      one - alternate character for set bits instr

      [edit]Exceptions

      3,4)std::out_of_range ifpos> str.size(),std::invalid_argument if any character is notone orzero.
      5)std::invalid_argument if any character is notone orzero.

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_constexpr_bitset202207L(C++23)A more constexprstd::bitset, overloads(3,5)
      __cpp_lib_bitset202306L(C++26)Interfacingstd::bitset withstd::string_view,(4)

      [edit]Example

      Run this code
      #include <bitset>#include <climits>#include <iostream>#include <string> int main(){// empty constructor (1)std::bitset<8> b1;// [0,0,0,0,0,0,0,0] // unsigned long long constructor (2)std::bitset<8> b2(42);// [0,0,1,0,1,0,1,0]std::bitset<70> bl(ULLONG_MAX);// [0,0,0,0,0,0,1,1,1,...,1,1,1] in C++11std::bitset<8> bs(0xfff0);// [1,1,1,1,0,0,0,0] // string constructor (3)std::string bit_string="110010";std::bitset<8> b3(bit_string);// [0,0,1,1,0,0,1,0]std::bitset<8> b4(bit_string,2);// [0,0,0,0,0,0,1,0]std::bitset<8> b5(bit_string,2,3);// [0,0,0,0,0,0,0,1] // string constructor using custom zero/one digits (3)std::string alpha_bit_string="aBaaBBaB";std::bitset<8> b6(alpha_bit_string,0, alpha_bit_string.size(),'a','B');// [0,1,0,0,1,1,0,1] // char* constructor using custom digits (5)std::bitset<8> b7("XXXXYYYY",8,'X','Y');// [0,0,0,0,1,1,1,1] std::cout<<"b1: "<< b1<<"\nb2: "<< b2<<"\nbl: "<< bl<<"\nbs: "<< bs<<"\nb3: "<< b3<<"\nb4: "<< b4<<"\nb5: "<< b5<<"\nb6: "<< b6<<"\nb7: "<< b7<<'\n';}

      Possible output:

      b1: 00000000b2: 00101010bl: 0000001111111111111111111111111111111111111111111111111111111111111111bs: 11110000b3: 00110010b4: 00000010b5: 00000001b6: 01001101b7: 00001111

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 396C++98the values of the zero and one characters for overload(3)
      were0 and1 (which do not correspond to'0' and'1')
      added parameters to provide
      values for these characters
      LWG 457C++98S wasCHAR_BIT* sizeof(unsignedlong)
      for overload(2), butunsignedlong is not
      guaranteed to use all its bits to represent its value
      consider the number
      of bits of the value
      representation instead
      LWG 2250C++98the behavior was undefined ifpos> str.size() istruealways throws an
      exception in this case

      [edit]See also

      sets bits totrue or given value
      (public member function)[edit]
      sets bits tofalse
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/bitset/bitset&oldid=178330"

      [8]ページ先頭

      ©2009-2025 Movatter.jp