Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      operator<<,>>(std::bitset)

      From cppreference.com
      <cpp‎ |utility‎ |bitset
       
       
      Utilities library
       
       
      Defined in header<bitset>
      template<class CharT,class Traits,std::size_t N>

      std::basic_ostream<CharT, Traits>&

          operator<<(std::basic_ostream<CharT, Traits>& os,conststd::bitset<N>& x);
      (1)
      template<class CharT,class Traits,std::size_t N>

      std::basic_istream<CharT, Traits>&

          operator>>(std::basic_istream<CharT, Traits>& is,std::bitset<N>& x);
      (2)

      Inserts or extracts a bitset from a character stream.

      1) Writes the bitsetx to the character streamos as if by first converting it to astd::basic_string<CharT, Traits> usingto_string(), and then writing it intoos using theoperator<< (which is aFormattedOutputFunction for strings).
      The characters to use for ones and zeroes are obtained from the currently-imbued locale by callingstd::use_facet<std::ctype<CharT>>(os.getloc()).widen() with'1' and'0' as arguments.
      2) Behaves as aFormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts up toN characters fromis and stores the characters in the bitsetx.
      Characters are extracted until either
      • N characters have been read,
      • end-of-file occurs inis, or
      • the next character is neitheris.widen('0') noris.widen('1').
      IfN>0 and no characters are extracted,is.setstate(ios_base::failbit) is called.

      Contents

      [edit]Parameters

      os - the character stream to write to
      is - the character stream to read from
      x - the bitset to be read or written

      [edit]Return value

      1)os
      2)is

      [edit]Example

      Run this code
      #include <bitset>#include <iostream>#include <sstream> int main(){std::string bit_string="001101";std::istringstream bit_stream(bit_string); std::bitset<3> b1;    bit_stream>> b1;// reads "001", stream still holds "101"std::cout<< b1<<'\n'; std::bitset<8> b2;    bit_stream>> b2;// reads "101", populates the 8-bit set as "00000101"std::cout<< b2<<'\n';}

      Output:

      00100000101

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 303C++98single-byte characters were extracted from
      is, butCharT can have multiple bytes
      extractsCharT and compares
      it with widened'0' and'1'
      LWG 396C++98the content written byoperator<< was locale-independentwrites widened'0's and'1's
      LWG 3199C++98extracting astd::bitset<0> always setsfailbitsuch extraction never setsfailbit

      [edit]See also

      performs binary shift left and shift right
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/bitset/operator_ltltgtgt2&oldid=176150"

      [8]ページ先頭

      ©2009-2025 Movatter.jp