bitset klass

Beskriver en typ av objekt som lagrar en sekvens som består av ett fast antal bitar som ger ett kompakt sätt att behålla flaggor för en uppsättning objekt eller villkor. Klassen bitset stöder åtgärder på objekt av typen bitset som innehåller en samling bitar och ger konstant åtkomst till varje bit.

Syntax

template <size_t N>
class bitset

Parameters

N
Anger antalet bitar i bitset objektet med ett heltal av typen size_t nonzero som måste vara känt vid kompileringstillfället.

Remarks

Till skillnad från den liknande vector<bool> klassenbitset har klassen inte iteratorer och är inte en C++-standardbibliotekscontainer. Det skiljer sig också från vector<bool> genom att vara av en viss specifik storlek som är fast vid kompileringstiden i enlighet med den storlek som anges av mallparametern N när deklareras bitset<N> .

En bit anges om dess värde är 1 och återställs om dess värde är 0. Att vända eller invertera en bit är att ändra dess värde från 1 till 0 eller från 0 till 1. N Bitarna i en bitset indexeras med heltalsvärden från 0 till N - 1, där 0 indexerar den första bitpositionen och N - 1 den sista bitpositionen.

Members

Constructors

Name Description
bitset Konstruerar ett objekt i klassen bitset<N> och initierar bitarna till noll, till ett visst angivet värde eller till värden som hämtas från tecken i en sträng.

Typedefs

Name Description
element_type En typ som är synonym för datatypen bool och som kan användas för att referera till elementbitar i en bitset.

Functions

Name Description
all Testar alla bitar i det här bitset för att avgöra om alla är inställda på true.
any Medlemsfunktionen testar om någon bit i sekvensen är inställd på 1.
count Medlemsfunktionen returnerar antalet bitar som angetts i bitsekvensen.
flip Invertera värdet för alla bitar i en bitset eller invertera en enskild bit vid en angiven position.
none Tester om ingen bit har angetts till 1 i ett bitset objekt.
reset Återställer alla bitar i en bitset till 0 eller återställer lite vid en angiven position till 0.
set Anger alla bitar i en bitset till 1 eller anger en bit vid en angiven position till 1.
size Returnerar antalet bitar i ett bitset objekt.
test Testar om biten på en angiven position i en bitset är inställd på 1.
to_string Konverterar ett bitset objekt till en strängrepresentation.
to_ullong Returnerar summan av bitvärdena i bitset som en unsigned long long.
to_ulong Konverterar ett bitset objekt till unsigned long som skulle generera sekvensen av bitar som ingår om det används för att initiera bitset.

Klasserna

Name Description
reference En proxyklass som innehåller referenser till bitar som finns i en bitset som används för att komma åt och manipulera enskilda bitar som en hjälpklass för operator[] klassen bitset.

Operatorer

Name Description
operator!= Testar ett mål bitset för ojämlikhet med en angiven bitset.
operator&= Gör en bitvis kombination av bituppsättningar med bitvis "och" (&) åtgärd.
operator<< Flyttar bitarna i en bitset till vänster ett angivet antal positioner och returnerar resultatet till en ny bitset.
operator<<= Flyttar bitarna i en bitset till vänster ett angivet antal positioner och returnerar resultatet till målet bitset.
operator== Testar ett mål bitset för likhet med en angiven bitset.
operator>> Flyttar bitarna i en bitset till höger ett angivet antal positioner och returnerar resultatet till en ny bitset.
operator>>= Flyttar bitarna i en bitset till höger ett angivet antal positioner och returnerar resultatet till målet bitset.
operator[] Returnerar en referens till en bit vid en angiven position i en bitset om den bitset kan ändras. Annars returneras värdet för biten vid den positionen.
operator^= Gör en bitvis kombination av bitmängder med den bitvis "xor" (^) åtgärden.
operator|= Gör en bitvis kombination av bitmängder med bitvis "eller" (|) åtgärd.
operator~ Invertera alla bitar i ett mål bitset och returnerar resultatet.

Structures

Name Description
hash

all

Testar alla bitar i den här bituppsättningen för att avgöra om alla är inställda på sant.

bool all() const;

Returvärde

Returnerar true om alla bitar i den här uppsättningen är sanna. Returnerar false om en eller flera bitar är falska.

any

Testar om någon bit i sekvensen är inställd på 1.

bool any() const;

Returvärde

true om någon bit i detta bitset är inställt på 1; false om alla bitar är 0.

Example

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 6 );
   bool b, rb;

   cout << "The original bitset b1( 6 ) is: ( "<< b1 << " )"
        << endl;

   b = b1.any ( );

   if ( b )
      cout << "At least one of the bits in bitset is set to 1."
           << endl;
   else
      cout << "None of the bits in bitset are set to 1."
           << endl;

   bitset<5> rb1;
   rb1 = b1.reset ( );

   cout << "The reset bitset is: ( "<< b1 << " )"
        << endl;

   rb = rb1.any ( );

   if ( rb )
      cout << "At least one of the bits in the reset bitset "
           << "are set to 1." << endl;
   else
      cout << "None of the bits in bitset b1 are set to 1."
           << endl;
}
The original bitset b1( 6 ) is: ( 00110 )
At least one of the bits in bitset is set to 1.
The reset bitset is: ( 00000 )
None of the bits in bitset b1 are set to 1.

bitset

Konstruerar ett objekt av klassen bitset<N> och initierar bitarna till noll, eller till något angivet värde, eller till värden som hämtas från tecken i en sträng.

1) constexpr bitset();
2) bitset(unsigned long val);
3) constexpr bitset(unsigned long long val);
4) template <class CharType, class Traits, class Allocator>
    explicit bitset(
        const basic_string<CharType, Traits, Allocator>& str,
        typename basic_string<CharType, Traits, Allocator>::size_type pos = 0);
    
5) template <class CharType, class Traits, class Allocator>
    explicit bitset(
        const basic_string<CharType, Traits, Allocator>& str,
        typename basic_string<CharType, Traits, Allocator>::size_type pos,
        typename basic_string<CharType, Traits, Allocator>::size_type count,
        CharType Zero = CharType ('0'),
        CharType One = CharType ('1'));
    
6) template<class CharType>
    explicit bitset(
        const CharType* str,
        typename basic_string<CharType>::size_type
        n = basic_string<CharType>::npos,
        CharType zero = CharType('0'),
        CharType one = CharType('1'));

Parameters

val
Det osignerade heltal vars grundläggande två representation används för att initiera bitarna i den bitset som skapas.

str
Strängen med nollor och de som används för att initiera bitset bitvärdena.

pos
Tecknets position i strängen, som räknar från vänster till höger och börjar med noll, används för att initiera den första biten bitseti .

count
Antalet tecken i strängen som används för att ange initiala värden för bitarna i bitset.

Zero
Tecknet som används för att representera en nolla. Standardvärdet är "0".

One
Tecknet som används för att representera ett. Standardvärdet är "1".

Remarks

1) Konstruerar ett objekt av klassen bitset<N> och initierar alla N-bitar till ett standardvärde på noll.

2-3) Konstruerar ett objekt av klassen bitset<N> och initierar bitarna från parametern val .

4) Konstruerar ett objekt av klassen bitset<N> och initierar bitar från tecknen i en sträng med nollor och ettor. Om några tecken i strängen är andra än 0 eller 1 genererar konstruktorn ett objekt av klassen invalid argument. Om den angivna positionen (pos) är längre än strängens längd genererar konstruktorn ett objekt av klassen out_of_range. Konstruktorn anger endast de bitar vid position j i bitset för vilken tecknet i strängen vid position pos + j är 1. Som standard pos är 0.

5) 4) Liknar men innehåller en annan parameter, count, som anger antalet bitar som ska initieras. Den har två valfria parametrar, _Zero och _One, som anger vilket tecken i str som ska tolkas som en 0-bitars respektive en 1-bitars.

6) Konstruerar ett objekt av klassen bitset<N>och initierar N-bitarna till värden som motsvarar tecknen i en teckensträng i C-stil med nollor och ettor. Du anropar konstruktorn utan att omvandla strängen till en strängtyp, till exempel: bitset<5> b5("01011");

Example

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

int main( )
{
   // Using the default constructor
   using namespace std;
   bitset<2> b0;
   cout << "The set of bits in bitset<2> b0 is: ( "
        << b0 << " )." << endl;

   // Using the second member function
   bitset<5> b1 ( 6 );
   cout << "The set of bits in bitset<5> b1( 6 ) is: ( "
        << b1 << " )." << endl;

   // The template parameter N can be an expression
   bitset< 2 * sizeof ( int ) > b2;
   cout << "The set of bits in bitset< 2 * sizeof ( int ) > b2 is: ( "
        << b2 << " )." << endl;

   // The base two representation will be truncated
   // if its length exceeds the size of the bitset
   bitset<3> b3 ( 6 );
   cout << "The set of bits in bitset<3> b3( 6 ) is ( "
        << b3 << " )." << endl;

   // Using a c-style string to initialize the bitset
    bitset<7> b3andahalf ( "1001001" );
    cout << "The set of bits in bitset<7> b3andahalf ( \"1001001\" )"
         << " is ( " << b3andahalf << " )." << endl;

   // Using the fifth member function with the first parameter
   string bitval4 ( "10011" );
   bitset<5> b4 ( bitval4 );
   cout << "The set of bits in bitset<5> b4( bitval4 ) is ( "
        << b4 << " )." << endl;

   // Only part of the string may be used for initialization

   // Starting at position 3 for a length of 6 (100110)
   string bitval5 ("11110011011");
   bitset<6> b5 ( bitval5, 3, 6 );
   cout << "The set of bits in bitset<11> b5( bitval, 3, 6 ) is ( "
        << b5 << " )." << endl;

   // The bits not initialized with part of the string
   // will default to zero
   bitset<11> b6 ( bitval5, 3, 5 );
   cout << "The set of bits in bitset<11> b6( bitval5, 3, 5 ) is ( "
        << b6 << " )." << endl;

   // Starting at position 2 and continue to the end of the string
   bitset<9> b7 ( bitval5, 2 );
   cout << "The set of bits in bitset<9> b7( bitval, 2 ) is ( "
        << b7 << " )." << endl;
}
The set of bits in bitset<2> b0 is: ( 00 ).
The set of bits in bitset<5> b1( 6 ) is: ( 00110 ).
The set of bits in bitset<2 * sizeof ( int ) > b2 is: ( 00000000 ).
The set of bits in bitset<3> b3( 6 ) is ( 110 ).
The set of bits in bitset<5> b4( bitval4 ) is ( 10011 ).
The set of bits in bitset<11> b5( bitval, 3, 6 ) is ( 100110 ).
The set of bits in bitset<11> b6( bitval5, 3, 5 ) is ( 00000010011 ).
The set of bits in bitset<9> b7( bitval, 2 ) is ( 110011011 ).

count

Returnerar antalet bitar som angetts i bitsekvensen.

size_t count() const;

Returvärde

Antalet bitar som angetts i bitsekvensen.

Example

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

int main( )
{
    using namespace std;

    bitset<5> b1(4);

    cout << "The collection of bits in the original bitset is: ( "
         << b1 << " )" << endl;

    size_t i;
    i = b1.count();
    cout << "The number of bits in the bitset set to 1 is: "
         << i << "." << endl;

    bitset<5> fb1;
    fb1 = b1.flip();

    cout << "The collection of flipped bits in the modified bitset "
         << "is: ( " << b1 << " )" << endl;

    size_t ii;
    ii = fb1.count();
    cout << "The number of bits in the bitset set to 1 is: "
         << ii << "." << endl;
}
The collection of bits in the original bitset is: ( 00100 )
The number of bits in the bitset set to 1 is: 1.
The collection of flipped bits in the modified bitset is: ( 11011 )
The number of bits in the bitset set to 1 is: 4.

element_type

En typ som är synonym för datatypen bool och som kan användas för att referera till elementbitar i en bitset.

typedef bool element_type;

Example

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

int main( )
{
   using namespace std;

   bitset<3> b1 ( 2 );
   cout << "Original bitset b1(6) is: ( "<< b1 << " )"
        << endl;

   //Compare two ways to reference bits in a bitset
   bool b;
   bitset<5>::element_type e;

   b = b1.test ( 2 );
   if ( b )
      cout << "The bit at position 2 of bitset b1"
           << "has a value of 1." << endl;
   else
      cout << "The bit at position 2 of bitset b1"
           << "has a value of 0." << endl;
   b1[2] = 1;
   cout << "Bitset b1 modified by b1[2] = 1 is: ( "<< b1 << " )"
        << endl;

   e = b1.test ( 2 );
   if ( e )
      cout << "The bit at position 2 of bitset b1"
           << "has a value of 1." << endl;
   else
      cout << "The bit at position 2 of bitset b1"
           << "has a value of 0." << endl;
}
Original bitset b1(6) is: ( 010 )
The bit at position 2 of bitset b1has a value of 0.
Bitset b1 modified by b1[2] = 1 is: ( 110 )
The bit at position 2 of bitset b1has a value of 1.

flip

Invertera värdet för alla bitar i en bitset eller invertera en enskild bit vid en angiven position.

bitset<N>& flip();
bitset<N>& flip(size_t pos);

Parameters

pos
Positionen för biten vars värde ska inverteras.

Returvärde

En kopia av den ändring bitset som medlemsfunktionen anropades för.

Remarks

Den andra medlemsfunktionen utlöser ett out_of_range undantag om den position som anges som en parameter är större än storleken på N vars bitset<N> bit inverterades.

Example

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

int main( )
{
   using namespace std;
   bitset<5> b1 ( 6 );

   cout << "The collection of bits in the original bitset is: ( "
        << b1 << " )" << endl;

   bitset<5> fb1;
   fb1 = b1.flip ( );

   cout << "After flipping all the bits, the bitset becomes: ( "
        << fb1 << " )" << endl;

   bitset<5> f3b1;
   f3b1 = b1.flip ( 3 );

   cout << "After flipping the fourth bit, the bitset becomes: ( "
        << f3b1 << " )" << endl << endl;

   bitset<5> b2;
   int i;
   for ( i = 0 ; i <= 4 ; i++ )
   {
      b2.flip(i);
      cout << b2 << "  The bit flipped is in position "
           << i << ".\n";
   }
}
The collection of bits in the original bitset is: ( 00110 )
After flipping all the bits, the bitset becomes: ( 11001 )
After flipping the fourth bit, the bitset becomes: ( 10001 )

00001  The bit flipped is in position 0.
00011  The bit flipped is in position 1.
00111  The bit flipped is in position 2.
01111  The bit flipped is in position 3.
11111  The bit flipped is in position 4.

hash

template <class T> struct hash;
template <size_t N> struct hash<bitset<N>>;

none

Tester om ingen bit har angetts till 1 i ett bitset objekt.

bool none() const;

Returvärde

true om ingen bit i bitset har angetts till 1. false Om minst en bit har angetts till 1.

Example

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 6 );
   bool b, rb;

   cout << "Original bitset b1(6) is: ( " << b1 << " )"
        << endl;

   b = b1.none ( );

   if ( b )
      cout << "None of the bits in bitset b1 are set to 1."
           << endl;
   else
      cout << "At least one of the bits in bitset b1 is set to 1."
           << endl;

   bitset<5> rb1;
   rb1 = b1.reset ( );
   rb = rb1.none ( );
   if ( rb )
      cout << "None of the bits in bitset b1 are set to 1."
           << endl;
   else
      cout << "At least one of the bits in bitset b1 is set to 1."
           << endl;
}
Original bitset b1(6) is: ( 00110 )
At least one of the bits in bitset b1 is set to 1.
None of the bits in bitset b1 are set to 1.

operator!=

Testar en målbituppsättning för ojämlikhet med en angiven bituppsättning.

bool operator!=(const bitset<N>& right) const;

Parameters

right
Det bitset som ska jämföras med målbituppsättningen för ojämlikhet.

Returvärde

true om bituppsättningarna är olika; false om de är likadana.

Remarks

Bituppsättningar måste ha samma storlek.

Example

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );
   bitset<5> b2 ( 7 );
   bitset<5> b3 ( 2 );
   bitset<4> b4 ( 7 );

   if ( b1 != b2 )
      cout << "Bitset b1 is different from bitset b2." << endl;
   else
      cout << "Bitset b1 is the same as bitset b2." << endl;

   if ( b1 != b3 )
      cout << "Bitset b1 is different from bitset b3." << endl;
   else
      cout << "Bitset b1 is the same as bitset b3." << endl;

   // This would cause an error because bitsets must have the
   // same size to be tested
   // if ( b1 != b4 )
   //   cout << "Bitset b1 is different from bitset b4." << endl;
   // else
   //   cout << "Bitset b1 is the same as bitset b4." << endl;
}
Bitset b1 is the same as bitset b2.
Bitset b1 is different from bitset b3.

operator&=

Gör en bitvis kombination av bituppsättningar med bitvis "och" (&) åtgärd.

bitset<N>& operator&=(const bitset<N>& right);

Parameters

right
Det bitset som ska kombineras bitvis med målbituppsättningen.

Returvärde

Den ändrade målbituppsättningen som är resultatet av bitvis "och"-åtgärden (&) med den bitset angivna som en parameter.

Remarks

Två bitar som kombineras med operatorn AND returnerar true om varje bit är sann. Annars returnerar falsederas kombination .

De två bituppsättningarna måste ha samma storlek.

Example

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );
   bitset<5> b2 ( 11 );
   bitset<4> b3 ( 7 );

   cout << "The target bitset b1 is:    ( "<< b1 << " )." << endl;
   cout << "The parameter bitset b2 is: ( "<< b2 << " )." << endl;
   cout << endl;

   b1 &= b2;
   cout << "After bitwise AND combination,\n"
        << "the target bitset b1 becomes:   ( "<< b1 << " )."
        << endl;

   // Note that the parameter-specified bitset is unchanged
   cout << "The parameter bitset b2 remains: ( "<< b2 << " )."
        << endl;

   // The following would cause an error because the bisets
   // must be of the same size to be combined
   // b1 &= b3;
}
The target bitset b1 is:    ( 00111 ).
The parameter bitset b2 is: ( 01011 ).

After bitwise AND combination,
the target bitset b1 becomes:   ( 00011 ).
The parameter bitset b2 remains: ( 01011 ).

operator<<

Flyttar bitarna i en bitset till vänster ett angivet antal positioner och returnerar resultatet till en ny bitset.

bitset<N> operator<<(size_t pos) const;

Parameters

pos
Antalet positioner till vänster som bitarna i bitset ska flyttas.

Returvärde

Den ändrade bituppsättningen med bitarna skiftade till vänster det antal positioner som krävs.

Remarks

Medlemsoperatorfunktionen returnerar bitset(*this) <<= pos där <<= flyttar bitarna i en bitset till vänster ett angivet antal positioner och returnerar resultatet till målet bitset.

Example

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );

   cout << "The bitset b1 is: ( "<< b1 << " )." << endl;

   bitset<5> b2;
   b2 = b1 << 2;

   cout << "After shifting the bits 2 positions to the left,\n"
        << " the bitset b2 is: ( "<< b2 << " )."
        << endl;

   bitset<5> b3 = b2 >> 1;

   cout << "After shifting the bits 1 position to the right,\n"
        << " the bitset b3 is: ( " << b3 << " )."
        << endl;
}

operator<<=

Flyttar bitarna i en bitset till vänster ett angivet antal positioner och returnerar resultatet till målet bitset.

bitset<N>& operator<<=(size_t pos);

Parameters

pos
Antalet positioner till vänster som bitarna i bitset ska flyttas.

Returvärde

Målet bitset ändrades så att bitarna har flyttats till vänster det antal positioner som krävs.

Remarks

Om det inte finns något element för att växla till positionen rensar funktionen biten till värdet 0.

Example

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

int main( )
{
   using namespace std;
   bitset<5> b1 ( 7 );
   cout << "The target bitset b1 is: ( "<< b1 << " )." << endl;
   b1 <<= 2;
   cout << "After shifting the bits 2 positions to the left,\n"
        << "the target bitset b1 becomes: ( "<< b1 << " )."
        << endl;
}
The target bitset b1 is: ( 00111 ).
After shifting the bits 2 positions to the left,
the target bitset b1 becomes: ( 11100 ).

operator==

Testar en målbituppsättning för likhet med en angiven bituppsättning.

bool operator==(const bitset<N>& right) const;

Parameters

right
Det bitset som ska jämföras med målbituppsättningen för likhet.

Returvärde

true om bituppsättningarna är samma; false om de är annorlunda.

Remarks

Bituppsättningar måste ha samma storlek.

Example

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

int main( )
{
   using namespace std;
   bitset<5> b1 ( 7 );
   bitset<5> b2 ( 7 );
   bitset<5> b3 ( 2 );
   bitset<4> b4 ( 7 );

   if ( b1 == b2 )
      cout << "Bitset b1 is the same as bitset b2." << endl;
   else
      cout << "Bitset b1 is different from bitset b2." << endl;

   if ( b1 == b3 )
      cout << "Bitset b1 is the same as bitset b3." << endl;
   else
      cout << "Bitset b1 is different from bitset b3." << endl;

   // This would cause an error because bitsets must have the
   // same size to be tested
   // if ( b1 == b4 )
   //   cout << "Bitset b1 is the same as bitset b4." << endl;
   // else
   //   cout << "Bitset b1 is different from bitset b4." << endl;
}
Bitset b1 is the same as bitset b2.
Bitset b1 is different from bitset b3.

operator>>

Flyttar bitarna i en bitset till höger ett angivet antal positioner och returnerar resultatet till en ny bituppsättning.

bitset<N> operator>>(size_t pos) const;

Parameters

pos
Antalet positioner till höger som bitarna i bitset ska flyttas.

Returvärde

En ny bituppsättning där bitarna har flyttats till höger det nödvändiga antalet positioner i förhållande till målet bitset.

Example

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

int main( )
{
   using namespace std;
   bitset<5> b1 ( 7 );
   cout << "The bitset b1 is: ( "<< b1 << " )." << endl;

   bitset<5> b2;
   b2 = b1 << 2;

   cout << "After shifting the bits 2 positions to the left,\n"
        << "the bitset b2 is: ( "<< b2 << " )."
        << endl;
   bitset<5> b3 = b2 >> 1;

   cout << "After shifting the bits 1 position to the right,\n"
        << "the bitset b3 is: ( " << b3 << " )."
        << endl;
}
The bitset b1 is: ( 00111 ).
After shifting the bits 2 positions to the left,
the bitset b2 is: ( 11100 ).
After shifting the bits 1 position to the right,
the bitset b3 is: ( 01110 ).

operator>>=

Flyttar bitarna i en bitset till höger ett angivet antal positioner och returnerar resultatet till målet bitset.

bitset<N>& operator>>=(size_t pos);

Parameters

pos
Antalet positioner till höger som bitarna i bitset ska flyttas.

Returvärde

bitset Målet ändrades så att bitarna har flyttats till höger det antal positioner som krävs.

Remarks

Om det inte finns något element för att växla till positionen rensar funktionen biten till värdet 0.

Example

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

int main( )
{
   using namespace std;
   bitset<5> b1 ( 28 );
   cout << "The target bitset b1 is: ( "<< b1 << " )." << endl;

   b1 >>= 2;
   cout << "After shifting the bits 2 positions to the right,\n"
        << "the target bitset b1 becomes: ( "<< b1 << " )."
        << endl;
}
The target bitset b1 is: ( 11100 ).
After shifting the bits 2 positions to the right,
the target bitset b1 becomes: ( 00111 ).

operator[]

Returnerar en referens till en bit vid en angiven position i en bitset om den bitset kan ändras. Annars returneras värdet för biten vid den positionen.

bool operator[](size_t pos) const;
reference operator[](size_t pos);

Parameters

pos
Positionen som placerar biten bitseti .

Remarks

När du definierar _ITERATOR_DEBUG_LEVEL som 1 eller 2 i din version uppstår ett körningsfel i den körbara filen om du försöker komma åt ett element utanför gränserna för bitset. Mer information finns i Kontrollerade iteratorer.

Example

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

int main( )
{
   using namespace std;
   bool b;
   bitset<5> b1 ( 6 );
   cout << "The initialized bitset<5> b1( 2 ) is: ( "<< b1 << " )."
        << endl;

   int i;
   for ( i = 0 ; i <= 4 ; i++ )
   {
      b = b1[ i ];
      cout << "  The bit in position "
           << i << " is " << b << ".\n";
   }
}

operator^=

Gör en bitvis kombination av bitmängder med den bitvis "xor" (^) åtgärden.

bitset<N>& operator^=(const bitset<N>& right);

Parameters

right
Det bitset som ska kombineras bitvis med målbituppsättningen.

Returvärde

Den ändrade målbituppsättningen som är resultatet av den bitvisa åtgärden "xor" (^) med den bitset angivna som en parameter.

Remarks

Två bitar som kombineras med den bitvis "xor"-operatorn (^) returnerar true om minst en, men inte båda, av bitarna är true; annars returnerar falsederas kombination .

Bituppsättningar måste ha samma storlek.

Example

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

int main( )
{
   using namespace std;
   bitset<5> b1 ( 7 );
   bitset<5> b2 ( 11 );
   bitset<4> b3 ( 7 );

   cout << "The target bitset b1 is:    ( "<< b1 << " )." << endl;
   cout << "The parameter bitset b2 is: ( "<< b2 << " )." << endl;
   cout << endl;

   b1 ^= b2;
   cout << "After bitwise exclusive OR combination,\n"
        << "the target bitset b1 becomes:   ( "<< b1 << " )."
        << endl;

   // Note that the parameter-specified bitset in unchanged
   cout << "The parameter bitset b2 remains: ( "<< b2 << " )."
        << endl;

   // The following would cause an error because the bitsets
   // must be of the same size to be combined
   // b1 |= b3;
}
The target bitset b1 is:    ( 00111 ).
The parameter bitset b2 is: ( 01011 ).

After bitwise exclusive OR combination,
the target bitset b1 becomes:   ( 01100 ).
The parameter bitset b2 remains: ( 01011 ).

operator|=

Kombinerar två bituppsättningar med hjälp av åtgärden "eller" (|).

bitset<N>& operator|=(const bitset<N>& right);

Parameters

right
Det bitset som ska kombineras bitvis med målet bitset.

Returvärde

Den ändrade målbituppsättningen som är resultatet av den bitvisa åtgärden "eller" (|) med den bitset angivna som en parameter.

Remarks

Två bitar som kombineras med den inkluderande OR operatorn returnerar true om minst en av bitarna är true. Om båda bitarna är falsereturnerar falsederas kombination .

Bituppsättningar måste ha samma storlek.

Example

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );
   bitset<5> b2 ( 11 );
   bitset<4> b3 ( 7 );

   cout << "The target bitset b1 is:    ( "<< b1 << " )." << endl;
   cout << "The parameter bitset b2 is: ( "<< b2 << " )." << endl;
   cout << endl;

   b1 |= b2;
   cout << "After bitwise inclusive OR combination,\n"
        << "the target bitset b1 becomes:   ( "<< b1 << " )."
        << endl;

   // Note that the parameter-specified bitset in unchanged
   cout << "The parameter bitset b2 remains: ( "<< b2 << " )."
        << endl;

   // The following would cause an error because the bisets
   // must be of the same size to be combined
   // b1 |= b3;
}
The target bitset b1 is:    ( 00111 ).
The parameter bitset b2 is: ( 01011 ).

After bitwise inclusive OR combination,
the target bitset b1 becomes:   ( 01111 ).
The parameter bitset b2 remains: ( 01011 ).

operator~

Invertera alla bitar i en målbituppsättning och returnerar resultatet.

bitset<N> operator~() const;

Returvärde

Med bitset alla dess bitar inverterade med avseende på målet bitset.

Example

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );
   bitset<5> b2;
   b2 = ~b1;

   cout << "Bitset b1 is: ( "<< b1 << " )." << endl;
   cout << "Bitset b2 = ~b1 is: ( "<< b2 << " )." << endl;

   // These bits could also be flipped using the flip member function
   bitset<5> b3;
   b3 = b1.flip( );
   cout << "Bitset b3 = b1.flip( ) is: ( "<< b2 << " )." << endl;
}
Bitset b1 is: ( 00111 ).
Bitset b2 = ~b1 is: ( 11000 ).
Bitset b3 = b1.flip( ) is: ( 11000 ).

reference

En proxyklass som innehåller referenser till bitar som finns i en bitset som används för att komma åt och manipulera enskilda bitar som en hjälpklass för operator[] klassen bitset.

class reference {
   friend class bitset<N>;
public:
   reference& operator=(bool val);
   reference& operator=(const reference& bitref);
   bool operator~() const;
   operator bool() const;
   reference& flip();
};

Parameters

val
Värdet för det objekt av typen som bool ska tilldelas till en bit i en bitset.

bitref
En referens av formuläret x [ i ] till biten vid position i i bitsetx.

Returvärde

En referens till biten i den bitset som anges av argumentpositionen för de första, andra och femte medlemsfunktionerna i klassreferensen, och true eller false, för att återspegla värdet för den ändrade biten bitset i för klassreferensens tredje och fjärde medlemsfunktioner.

Remarks

Klassen reference finns bara som en hjälpklass för bitsetoperator[]. Medlemsklassen beskriver ett objekt som kan komma åt en enskild bit i en bitset. Låt vara b ett objekt av typen bool, x och y objekt av typen bitset<N>och ij giltiga positioner i ett sådant objekt. Notationen x [i] refererar till biten vid position i i bituppsättningen x. Medlemsfunktionerna i klassen reference tillhandahåller följande åtgärder i ordning:

Operation Definition
x[i] = b Lagrar bool värdet b vid bitposition i i bituppsättningen x.
x[i] = y[j] Lagrar värdet för biten y[ j] vid bitposition i i bituppsättningen x.
b = ~ x[i] Lagrar det vända värdet för biten x[ i] i boolb.
b = x[i] Lagrar värdet för biten x[ i] i boolb.
x[i]. flip( ) Lagrar det vända värdet för biten x[ i] tillbaka vid bitposition i i x.

Example

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 2 );
   bitset<5> b2 ( 6 );
   cout << "The initialized bitset<5> b1( 2 ) is: ( "<< b1 << " )."
        << endl;
   cout << "The initialized bitset<5> b2( 6 ) is: ( "<< b2 << " )."
        << endl;

   // Example of x [i] = b storing bool b at bit position i
   // in bitset x
   b1[ 0 ] = true;
   cout << "The bitset<5> b1 with the bit at position 0 set to 1"
        << "is: ( "<< b1 << " )" << endl;

   // Example of x [i] = y [j] storing the bool value of the
   // bit at position j in bitset y at bit position i in bitset x
   b2 [4] = b1 [0];      // b1 [0] = true
   cout << "The bitset<5> b2 with the bit at position 4 set to the "
        << "value\nof the bit at position 0 of the bit in "
        << "bitset<5> b1 is: ( "<<  b2  << " )" << endl;

   // Example of b = ~x [i] flipping the value of the bit at
   // position i of bitset x and storing the value in an
   // object b of type bool
   bool b = ~b2 [4];      // b2 [4] = false
   if ( b )
      cout << "The value of the object b = ~b2 [4] "
           << "of type bool is true." << endl;
   else
      cout << "The value of the object b = ~b2 [4] "
           << "of type bool is false." << endl;

   // Example of b = x [i] storing the value of the bit at
   // position i of bitset x in the object b of type bool
   b = b2 [4];
   if ( b )
      cout << "The value of the object b = b2 [4] "
           << "of type bool is true." << endl;
   else
      cout << "The value of the object b = b2 [4] "
           << "of type bool is false." << endl;

   // Example of x [i] . flip ( ) toggling the value of the bit at
   // position i of bitset x
   cout << "Before flipping the value of the bit at position 4 in "
        << "bitset b2,\nit is ( "<<  b2  << " )." << endl;
   b2 [4].flip( );
   cout << "After flipping the value of the bit at position 4 in "
        << "bitset b2,\nit becomes ( "<<  b2  << " )." << endl;
   bool c;
   c = b2 [4].flip( );
   cout << "After a second flip, the value of the position 4 "
        << "bit in b2 is now: " << c << ".";
}
The initialized bitset<5> b1( 2 ) is: ( 00010 ).
The initialized bitset<5> b2( 6 ) is: ( 00110 ).
The bitset<5> b1 with the bit at position 0 set to 1 is: ( 00011 )
The bitset<5> b2 with the bit at position 4 set to the value
of the bit at position 0 of the bit in bitset<5> b1 is: ( 10110 )
The value of the object b = ~b2 [4] of type bool is false.
The value of the object b = b2 [4] of type bool is true.
Before flipping the value of the bit at position 4 in bitset b2,
it is ( 10110 ).
After flipping the value of the bit at position 4 in bitset b2,
it becomes ( 00110 ).
After a second flip, the value of the position 4 bit in b2 is now: 1.

reset

Återställer alla bitar i en bitset till 0 eller återställer lite vid en angiven position till 0.

bitset<N>& reset();
bitset<N>& reset(size_t pos);

Parameters

pos
Positionen för biten i som bitset ska återställas till 0.

Returvärde

En kopia av den bitset som medlemsfunktionen anropades för.

Remarks

Den andra medlemsfunktionen utlöser ett out_of_range undantag om den angivna positionen är större än storleken på bitset.

Example

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

int main( )
{
   using namespace std;

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

   bitset<5> b1r3;
   b1r3 = b1.reset( 2 );
   cout << "The collection of bits obtained from resetting the\n"
        << "third bit of bitset b1 is: ( "<< b1r3 << " )"
        << endl;

   bitset<5> b1r;
   b1r = b1.reset( );
   cout << "The collecion of bits obtained from resetting all\n"
        << "the elements of the bitset b1 is: ( "<< b1r << " )"
        << endl;
}
The set of bits in bitset<5> b1(13) is: ( 01101 )
The collecion of bits obtained from resetting the
third bit of bitset b1 is: ( 01001 )
The collecion of bits obtained from resetting all
the elements of the bitset b1 is: ( 00000 )

set

Anger alla bitar i en bitset till 1 eller anger en bit vid en angiven position till 1.

bitset<N>& set();

bitset<N>& set(
    size_t pos,
    bool val = true);

Parameters

pos
Positionen för biten i som bitset ska ange det tilldelade värdet.

val
Värdet som ska tilldelas biten på den angivna positionen.

Returvärde

En kopia av den bitset som medlemsfunktionen anropades för.

Remarks

Den andra medlemsfunktionen utlöser ett out_of_range undantag om den angivna positionen är större än storleken på 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 )

size

Returnerar antalet bitar i ett bitset objekt.

size_t size() const;

Returvärde

Antalet bitar, N, i en bitset<N>.

Example

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

int main()
{
    using namespace std;

    bitset<5> b1(6);
    size_t i;

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

    i = b1.size();

    cout << "The number of bits in bitset b1 is: " << i << "."
         << endl;
}
The set of bits in bitset<5> b1( 6 ) is: ( 00110 )
The number of bits in bitset b1 is: 5.

test

Testar om biten på en angiven position i en bitset är inställd på 1.

bool test(size_t pos) const;

Parameters

pos
Positionen för biten i som bitset ska testas för dess värde.

Returvärde

trueom den bit som anges av argumentpositionen är inställd på 1; annars . false

Remarks

Medlemsfunktionen genererar en out_of_range

to_string

Konverterar ett bitset objekt till en strängrepresentation.

template <class charT = char, class traits = char_traits<charT>, class Allocator = allocator<charT> >
   basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;

Returvärde

Ett strängobjekt i klassen basic_string, där varje bit som anges i bitset har motsvarande tecken 1 och ett tecken på 0 om biten är oet.

Example

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );

   cout << "The ordered set of bits in the bitset<5> b1( 7 )"
        << "\n  that was generated by the number 7 is: ( "
        << b1 << " )" << endl;

   string s1;
   s1 =  b1.template to_string<char,
   char_traits<char>, allocator<char> >( );
   cout << "The string returned from the bitset b1"
        << "\n  by the member function to_string( ) is: "
        << s1 << "." << endl;
}
The ordered set of bits in the bitset<5> b1( 7 )
  that was generated by the number 7 is: ( 00111 )
The string returned from the bitset b1
  by the member function to_string( ) is: 00111.

to_ullong

Returnerar ett unsigned long long värde som innehåller samma bitar som objektets bitset innehåll.

unsigned long long to_ullong() const;

Returvärde

Returnerar summan av bitvärdena som finns i bitsekvensen som en unsigned long long. Det här unsigned long long värdet återskapar samma uppsättning bitar om det används för att initiera en bitset.

Exceptions

Genererar ett overflow_error objekt om någon bit i bitsekvensen har ett bitvärde som inte kan representeras som ett värde av typen unsigned long long.

Remarks

Returnerar summan av bitvärdena som finns i bitsekvensen som en unsigned long long.

to_ulong

Konverterar ett bitset objekt till heltalet som skulle generera sekvensen med bitar som ingår om det används för att initiera bitset.

unsigned long to_ulong( ) const;

Returvärde

Ett heltal som skulle generera bitarna i en bitset om det används vid initieringen av bitset.

Remarks

Om medlemsfunktionen tillämpas returneras heltalet som har samma sekvens med 1 och 0 siffror som finns i sekvensen med bitar som finns i bitset.

Medlemsfunktionen genererar ett overflow_error objekt om någon bit i bitsekvensen har ett bitvärde som inte kan representeras som ett värde av typen unsigned long.

Example

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

int main( )
{
   using namespace std;

   bitset<5> b1 ( 7 );

   cout << "The ordered set of bits in the bitset<5> b1( 7 )"
        << "\n  that was generated by the number 7 is: ( "
        << b1 << " )" << endl;

   unsigned long int i;
   i = b1.to_ulong( );
   cout << "The integer returned from the bitset b1,"
        << "\n  by the member function to_long( ), that"
        << "\n  generated the bits as a base two number is: "
        << i << "." << endl;
}
The ordered set of bits in the bitset<5> b1( 7 )
  that was generated by the number 7 is: ( 00111 )
The integer returned from the bitset b1,
  by the member function to_long( ), that
  generated the bits as a base two number is: 7.