operator<<,>>(std::bitset)
來自 cppreference.com
定義於標頭檔案 <bitset> |
||
template< class CharT, class Traits, std::size_t N > std::basic_ostream<CharT, Traits>& |
(1) | |
template< class CharT, class Traits, std::size_t N > std::basic_istream<CharT, Traits>& |
(2) | |
將 bitset 插入或提取到字元流中。
1) 將 bitset x 寫入字元流 os,如同先將其轉換為 std::basic_string<CharT, Traits>(使用 to_string()),然後使用 operator<<(它是字串的 FormattedOutputFunction)將其寫入 os。
用於表示一和零的字元透過呼叫 std::use_facet<std::ctype<CharT>>(os.getloc()).widen() 並以 '1' 和 '0' 作為引數,從當前所浸潤的區域設定中獲取。
用於表示一和零的字元透過呼叫 std::use_facet<std::ctype<CharT>>(os.getloc()).widen() 並以 '1' 和 '0' 作為引數,從當前所浸潤的區域設定中獲取。
2) 表現為 FormattedInputFunction。在構造並檢查哨兵物件(可能跳過前導空白)後,從 is 中提取最多 N 個字元,並將這些字元儲存在 bitset x 中。
提取字元直到滿足以下任一條件:
提取字元直到滿足以下任一條件:
- 已讀取 N 個字元,
- is 中發生檔案結束,或
- 下一個字元既不是 is.widen('0') 也不是 is.widen('1')。
目錄 |
[編輯] 引數
os | - | 要寫入的字元流 |
is | - | 要讀取的字元流 |
x | - | 要讀取或寫入的 bitset |
[編輯] 返回值
1) os
2) is
[編輯] 示例
執行此程式碼
#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'; }
輸出
001 00000101
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 303 | C++98 | 從 is 提取單位元組字元,但 CharT 可以有多個位元組從 is 提取單位元組字元,但 CharT 可以有多個位元組 |
提取 CharT 並將其與加寬的 '0' 和 '1' 進行比較提取 CharT 並將其與加寬的 '0' 和 '1' 進行比較 |
LWG 396 | C++98 | operator<< 寫入的內容與區域設定無關 | 寫入加寬的 '0' 和 '1' |
LWG 3199 | C++98 | 提取 std::bitset<0> 總是設定 failbit |
此類提取從不設定 failbit |
[編輯] 參閱
執行二進位制左移和右移 (公共成員函式) |