|
bitset ( );
bitset ( unsigned long val );
template<class charT, class traits, class Allocator>
explicit bitset ( const basic_string<charT,traits,Allocator>& str,
typename basic_string<charT,traits,Allocator>::size_type pos = 0,
typename basic_string<charT,traits,Allocator>::size_type n =
basic_string<charT,traits,Allocator>::npos);
Construct bitset
Constructs a bitset container object.
If the default constructor is used, the bitset is initialized with zeros, otherwise its initial content depends on the parameters used:
Bitsets have a fixed size, which is determined by their class's template parameter:
|
template <size_t N> class bitset;
|
Where N is the size, specified as an integer value of type size_t.
Parameters
- val
- Integral value whose bits are copied to the bitset elements, up to the smaller of the bitset size and the size in bits of an unsigned long.
- str
- String containing a binary value (zeros and ones). These are parsed and used to initizialize the bits. If some character other than zero or one is found, the function throws an invalid_argument exception.
- pos
- First character in the string to be read as the content for the bitset.
- n
- Number of characters to read. A value of string::npos specifies that as many characters as possible are to be read.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
// constructing bitsets
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
int main ()
{
bitset<10> first; // empty bitset
bitset<10> second (120ul); // initialize from unsigned long
bitset<10> third (string("01011")); // initialize from string
return 0;
}
|
The code does not produce any output, but demonstrates some ways in which a bitset container can be constructed.
See also
|