std::bitset::bitset
STD::位集::位集
| (1) | |
---|---|---|
bitset( | (until C++11) | |
constexpr bitset( | (since C++11) | |
| (2) | |
bitset( unsigned long val | (until C++11) | |
constexpr bitset( unsigned long long val | (since C++11) | |
| (3) | |
template< class CharT, class Traits, class Alloc > explicit bitset( const std::basic_string<CharT,Traits,Alloc>& str, typename std::basic_string<CharT,Traits,Alloc>::size_type pos = 0, typename std::basic_string<CharT,Traits,Alloc>::size_type n = std::basic_string<CharT,Traits,Alloc>::npos | (until C++11) | |
template< class CharT, class Traits, class Alloc > explicit bitset( const std::basic_string<CharT,Traits,Alloc>& str, typename std::basic_string<CharT,Traits,Alloc>::size_type pos = 0, typename std::basic_string<CharT,Traits,Alloc>::size_type n = std::basic_string<CharT,Traits,Alloc>::npos, CharT zero = CharT('0'), CharT one = CharT('1') | (since C++11) | |
template< class CharT > explicit bitset( const CharT* str, typename std::basic_string<CharT>::size_type n = std::basic_string<CharT>::npos, CharT zero = CharT('0'), CharT one = CharT('1') | (4) | (since C++11) |
从几个可选数据源中的一个构造一个新的位集:
1%29默认构造函数。构造一个位集,将所有位设置为零。
2%29构造一个位集,初始化第一个%28最右边,最不重要的%29M
的相应位值的位位置。val
,在哪里M
是unsigned long long
位数N
在正在构建的位集中。如果M
小于N
%28,则位集大于32%28,直到C++11%2964%28自C++11%29位开始,对于无符号长%28的典型实现,C++11%29长%29,其余位位置被初始化为零。
3%29使用std::basic_string
str
.一个可选的起始位置pos
和长度n
可以提供,以及表示SET%28的替换值的字符。one
%29和未设置%28zero
%29位Traits::eq()
用于比较字符值。
初始化字符串的有效长度为最小%28。n
,,,str.size() - pos
29%。
如果pos > str.size(),此构造函数抛出std::out_of_range.如有任何字符在str不是zero或one,它抛出std::invalid_argument...
4%29与%283%29相似,但使用CharT*而不是std::basic_string相当于bitset(n == basic_string<CharT>::npos ? basic_string<CharT>(str) : basic_string<CharT>(str, n), 0, n, zero, one)
参数
val | - | number used to initialize the bitset |
---|---|---|
str | - | string used to initialize the bitset |
pos | - | a starting offset into str |
n | - | number of characters to use from str |
one | - | alternate character for set bits in str |
zero | - | alternate character for unset bits in str |
例外
1) none 2) none | (until C++11) |
---|---|
1) noexcept specification: noexcept 2) noexcept specification: noexcept | (since C++11) |
3%29std::out_of_range如果pos > str.size(),,,std::invalid_argument如果任何字符不是1或0
4%29std::invalid_argument
如果任何字符不是1或0
例
二次
#include <bitset>
#include <string>
#include <iostream>
#include <climits>
int main()
{
// empty constructor
std::bitset<8> b1; // [0,0,0,0,0,0,0,0]
// unsigned long long constructor
std::bitset<8> b2(42 // [0,0,1,0,1,0,1,0]
std::bitset<70> bl(ULLONG_MAX // [0,0,0,0,0,0,1,1,1,...,1,1,1] in C++11
std::bitset<8> bs(0xfff0 // [1,1,1,1,0,0,0,0]
// string constructor
std::string bit_string = "110010";
std::bitset<8> b3(bit_string // [0,0,1,1,0,0,1,0]
std::bitset<8> b4(bit_string, 2 // [0,0,0,0,0,0,1,0]
std::bitset<8> b5(bit_string, 2, 3 // [0,0,0,0,0,0,0,1]
// string constructor using custom zero/one digits
std::string alpha_bit_string = "aBaaBBaB";
std::bitset<8> b6(alpha_bit_string, 0, alpha_bit_string.size(),
'a', 'B' // [0,1,0,0,1,1,0,1]
// char* constructor using custom digits
std::bitset<8> b7("XXXXYYYY", 8, 'X', 'Y' // [0,0,0,0,1,1,1,1]
std::cout << b1 << '\n' << b2 << '\n' << bl << '\n' << bs << '\n'
<< b3 << '\n' << b4 << '\n' << b5 << '\n' << b6 << '\n'
<< b7 << '\n';
}
二次
可能的产出:
二次
00000000
00101010
0000001111111111111111111111111111111111111111111111111111111111111111
11110000
00110010
00000010
00000001
01001101
00001111
二次
另见
set | sets bits to true or given value (public member function) |
---|---|
reset | sets bits to false (public member function) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。