std::bitset<N>::to_string
來自 cppreference.com
(1) | ||
template< class CharT, class Traits, class Allocator > std::basic_string<CharT, Traits, Allocator> |
(C++11 前) | |
template< class CharT = char, |
(C++11 起) (C++23 起 constexpr) |
|
template< class CharT, class Traits > std::basic_string<CharT, Traits> |
(2) | (C++11 前) |
template< class CharT > std::basic_string<CharT> to_string( CharT zero = CharT('0'), |
(3) | (C++11 前) |
std::string to_string( char zero = '0', char one = '1' ) const; |
(4) | (C++11 前) |
將 bitset 的內容轉換為字串。使用 zero 表示值為 false 的位,使用 one 表示值為 true 的位。
生成的字串包含 N 個字元,其中第一個字元對應於最後一個(N-1th)位,最後一個字元對應於第一個位。
所有模板型別實參都需要提供,因為函式模板不能有預設模板實參。提供了過載 (2-4) 以簡化 2) 使用預設分配器 std::allocator。
|
(C++11 前) |
目錄 |
[edit] 引數
零 | - | 用於表示 false 的字元 |
一 | - | 用於表示 true 的字元 |
[edit] 返回值
1) 轉換後的字串。
2) to_string<CharT, Traits, std::allocator<CharT>>(zero, one)。
3) to_string<CharT, std::char_traits<CharT>, std::allocator<CharT>>(zero, one)。
4) to_string<char, std::char_traits<char>, std::allocator<char>>(zero, one)。
[edit] 異常
可能從 std::basic_string 建構函式丟擲 std::bad_alloc。
[edit] 注意
自 C++11 起,函式模板可以有預設模板實參。LWG issue 1113 移除了輔助過載 (2-4) 並在 (1) 中添加了相應的預設模板實參。
[edit] 示例
執行此程式碼
#include <bitset> #include <iostream> int main() { std::bitset<8> b{42}; std::cout << b.to_string() << '\n' << b.to_string('*') << '\n' << b.to_string('O', 'X') << '\n'; }
輸出
00101010 **1*1*1* OOXOXOXO
[edit] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 396 | C++98 | 零位和一位被轉換為字元 0 和 1(不對應 '0' 和 '1') |
新增引數以提供 這些字元的值 |
LWG 434 | C++98 | 所有模板引數都需要提供 | 新增過載 (2-4) |
LWG 853 | C++98 | 過載 (2-4) 沒有 LWG issue 396 新增的預設 引數 |
也添加了 |
[edit] 參見
返回資料的 unsigned long 整型表示 (公共成員函式) | |
(C++11) |
返回資料的 unsigned long long 整型表示 (公共成員函式) |