Cpp-standard-library-cpp-bitset-operator-insertion

提供:Dev Guides
移動先:案内検索

C ++ビットセットライブラリ-operator >>関数

説明

C ++関数 *std
bitset :: operator >>* は、ビットストリーム `+ x `を文字ストリーム ` os +`に挿入します。

宣言

以下は、std
bitsetヘッダーからのstd :: bitset :: operator >>関数の宣言です。

C 98

template<class charT, class traits, size_t N>
basic_ostream<charT, traits>&
operator<< (basic_ostream<charT,traits>& os, const bitset<N>& x);

C 11

template<class charT, class traits, size_t N>
basic_ostream<charT, traits>&
operator<< (basic_ostream<charT,traits>& os, const bitset<N>& x);

パラメーター

  • os -書き込み先の文字ストリーム。
  • x -書き込まれるビットセット。

戻り値

操作された文字ストリーム、つまり + os +

例外

例外が発生した場合、すべてのオブジェクトは有効な状態のままです。

次の例は、std
bitset :: operator >>関数の使用法を示しています。
#include <iostream>
#include <bitset>
#include <sstream>

using namespace std;

int main(void) {

   string s = "101010";
   istringstream stream(s);
   bitset<2> b1;
   bitset<6> b2;

  /*Store first 2 bits*/
   stream >> b1;
   cout << "b1 = " << b1 << endl;

  /*Stores next 4 bits*/
   stream >> b2;
   cout << "b2 = " << b2 << endl;

   return 0;
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

b1 = 10
b2 = 001010