共用方式為


bitset::set

Sets all the bits in a bitset to 1 or sets a bit at a specified position to 1.

bitset<N>& set( ); 
bitset<N>& set( 
   size_t _Pos,  
   bool _Val = true 
);

Parameters

  • _Pos
    The position of the bit in the bitset to be set to assigned a value.

  • _Val
    The value to be assigned to the bit at the position specified.

Return Value

A copy of the bitset for which the member function was invoked.

Remarks

The second member function throws an out_of_range exception if the position specified is greater than the size of the bitset.

Example

// bitset_set.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;

   bitset<5> b1 ( 6 );
   cout << "The set of bits in bitset<5> b1(6) is: ( "<< b1 << " )"
        << endl;

   bitset<5> b1s0;
   b1s0 = b1.set( 0 );
   cout << "The collecion of bits obtained from setting the\n"
        << " zeroth bit of bitset b1 is: ( "<< b1s0 << " )" 
        << endl;

   bitset<5> bs1;
   bs1 = b1.set( );
   cout << "The collecion of bits obtained from setting all the\n"
        << " elements of the bitset b1 is: ( "<< bs1 << " )"
        << endl;
}
The set of bits in bitset<5> b1(6) is: ( 00110 )
The collecion of bits obtained from setting the
 zeroth bit of bitset b1 is: ( 00111 )
The collecion of bits obtained from setting all the
 elements of the bitset b1 is: ( 11111 )

Requirements

Header: <bitset>

Namespace: std

See Also

Reference

bitset Class