名前空間
変種
操作

std::bitset<N>::count

提供: cppreference.com
< cpp‎ | utility‎ | bitset
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
 
std::size_t count() const;
(C++11未満)
std::size_t count() const noexcept;
(C++11以上)

true に設定されているビットの数を返します。

目次

[編集] 引数

(なし)

[編集] 戻り値

true に設定されているビットの数。

[編集]

#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

[編集] 関連項目

ビットセットが保持できるビットの数を返します
(パブリックメンバ関数) [edit]