C++ Bitset 库 - operator>> 函数
描述
C++ 函数 std::bitset::operator>> 将位集 x 插入字符流 os。
声明
以下是 std::bitset::operator>> 函数形式 std::bitset 标头的声明。
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

