cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : bitset : count
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
bitset
bitset::bitset
bitset operators
member functions:
· bitset::any
· bitset::count
· bitset::flip
· bitset::none
· bitset::operator[]
· bitset::reset
· bitset::set
· bitset::size
· bitset::test
· bitset::to_string
· bitset::to_ulong

-

bitset::count public member function
size_t count ( );

Count bits set

Returns the amount of bits in the bitset that are set (i.e., have a value of 1).

Parameters

none

Return value

The number of bits set.

size_t is an unsigned integral type.

Example

// bitset::count
#include <iostream>
#include <string>
#include <bitset>
using namespace std;

int main ()
{
  bitset<8> myset (string("10110011"));

  cout << "myset has " << int(myset.count()) << " ones ";
  cout << "and " << int(myset.size()-myset.count()) << " zeros.\n";

  return 0;
}

Output:

myset has 5 ones, and 3 zeros.

See also

bitset::size Return size (public member function)
bitset::any Test if any bit is set (public member function)
bitset::none Test if no bit is set (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us