std::bitset<N>::count
提供: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
std::size_t count() const; |
(C++11未満) | |
std::size_t count() const noexcept; |
(C++11以上) | |
true に設定されているビットの数を返します。
引数
(なし)
戻り値
true に設定されているビットの数。
例
Run this code
#include <iostream>
#include <bitset>
int main()
{
std::bitset<8> b("00010010");
std::cout << "initial value: " << b << '\n';
// find the first unset bit
std::size_t idx = 0;
while (idx < b.size() && b.test(idx)) ++idx;
// continue setting bits until half the bitset is filled
while (idx < b.size() && b.count() < b.size()/2) {
b.set(idx);
std::cout << "setting bit " << idx << ": " << b << '\n';
while (idx < b.size() && b.test(idx)) ++idx;
}
}
出力:
initial value: 00010010
setting bit 0: 00010011
setting bit 2: 00010111
関連項目
| ビットセットが保持できるビットの数を返します (パブリックメンバ関数) |