bitset
sınıfı
Bir dizi öğe veya koşul için bayrak tutmanın küçük bir yolunu sağlayan sabit sayıda bit içeren bir dizi depolayan nesne türünü açıklar. sınıfı, bitset
bit koleksiyonu içeren ve her bite sabit zamanlı erişim sağlayan bit kümesi türündeki nesneler üzerinde işlemleri destekler.
Sözdizimi
template <size_t N>
class bitset
Parametreler
N
Derleme zamanında bilinmesi gereken türü sıfır olmayan tamsayıya size_t
sahip nesnedeki bitset
bit sayısını belirtir.
Açıklamalar
Benzer vector<bool>
Sınıftan farklı olarak, sınıfın bitset
yineleyicileri yoktur ve C++ Standart Kitaplığı kapsayıcısı değildir. Ayrıca, belirtilen şablon parametresi N
bitset<N>
tarafından belirtilen boyuta uygun olarak derleme zamanında sabitlenmiş belirli bir boyutta olmasından da farklıdırvector<bool>
.
Değeri 1 ise bit ayarlanır ve değeri 0 ise sıfırlanır. Bir biti çevirmek veya ters çevirmek, değerini 1'den 0'a veya 0'dan 1'e değiştirmektir. içindeki N
bitset
bitler 0 N
ile - 1 arasında tamsayı değerlerine göre dizinlenir; burada 0 ilk bit konumunu ve N
- 1 son bit konumunu dizinler.
Üyeler
Oluşturucular
Veri Akışı Adı | Açıklama |
---|---|
bitset |
sınıfının bitset<N> nesnesini oluşturur ve bitleri sıfıra, belirli bir değere veya dizedeki karakterlerden alınan değerlere başlatır. |
Tür tanımları
Veri Akışı Adı | Açıklama |
---|---|
element_type |
Veri türü için eş anlamlı olan ve içindeki öğe bitlerine başvurmak için kullanılabilen bir bitset türbool . |
İşlevler
Veri Akışı Adı | Açıklama |
---|---|
all |
Tüm bitlerin olarak ayarlanıp ayarlanmadığını belirlemek için bu bitset içindeki tüm bitleri test eder true . |
any |
Üye işlevi, dizideki herhangi bir bitin 1 olarak ayarlanıp ayarlanmadığını sınar. |
count |
Üye işlevi, bit dizisinde ayarlanan bit sayısını döndürür. |
flip |
Bir içindeki tüm bitlerin bitset değerini ters çevirir veya belirtilen konumda tek bir biti ters çevirir. |
none |
Nesnede bitset bit 1 olarak ayarlanmamışsa sınar. |
reset |
Bir içindeki tüm bitleri 0'a bitset sıfırlar veya belirli bir konumdaki bir biti 0 olarak sıfırlar. |
set |
Bir bitset içindeki tüm bitleri 1 olarak ayarlar veya belirli bir konumdaki bir biti 1 olarak ayarlar. |
size |
Nesnedeki bitset bit sayısını döndürür. |
test |
içinde belirtilen konumdaki bitset bitin 1 olarak ayarlanıp ayarlanmadığını sınar. |
to_string |
Nesneyi dize bitset gösterimine dönüştürür. |
to_ullong |
içindeki bitset bit değerlerinin toplamını olarak unsigned long long döndürür. |
to_ulong |
bir bitset nesneyi, başlatmak için unsigned long kullanılırsa içerdiği bit dizisini oluşturacak olan nesnesine bitset dönüştürür. |
Sınıflar
Veri Akışı Adı | Açıklama |
---|---|
reference |
sınıfının yardımcı sınıfı olarak tek tek bitlere erişmek ve işlemek için kullanılan bir bitset içinde yer alan bitlere başvurular sağlayan bir ara sunucu sınıfıbitset .operator[] |
İşleçler
Veri Akışı Adı | Açıklama |
---|---|
operator!= |
Belirtilen bitset ile bir hedefi bitset eşitsizliğe karşı test ediyor. |
operator&= |
Bit tabanlı "ve" (& ) işlemiyle bit kümeleri bileşimini yapar. |
operator<< |
bir bitset içindeki bitleri belirtilen sayıda konumu sola kaydırarak sonucu yeni bitset bir konumuna döndürür. |
operator<<= |
bir bitset içindeki bitleri belirtilen sayıda konumu sola kaydırıp sonucu hedeflenen bitset konumuna döndürür. |
operator== |
Belirtilen ile bir hedefi bitset eşitlik için test ediyor bitset . |
operator>> |
bir bitset içindeki bitleri belirtilen sayıda konumu sağa kaydırarak sonucu yeni bitset bir konumuna döndürür. |
operator>>= |
bir bitset içindeki bitleri belirtilen sayıda konumu sağa kaydırarak sonucu hedeflenen bitset öğesine döndürür. |
operator[] |
değeri değiştirilebilirsebitset , belirtilen konumdaki bir bitset bite başvuru döndürür; aksi takdirde, bitin değerini bu konumda döndürür. |
işleç^= | Bit tabanlı "xor" (^ ) işlemiyle bit kümeleri bileşimini yapar. |
operator|= |
Bit tabanlı "veya" (| ) işlemiyle bit kümelerinin bit düzeyinde bir birleşimini yapar. |
operator~ |
Bir hedefteki bitset tüm bitleri ters çevirir ve sonucu döndürür. |
Yapılar
Veri Akışı Adı | Açıklama |
---|---|
hash |
all
Tümünün true olarak ayarlı olup olmadığını belirlemek için bu bit kümesindeki tüm bitleri test eder.
bool all() const;
Dönüş Değeri
Bu kümedeki tüm bitler true ise döndürür true
. Bir veya daha fazla bit yanlışsa döndürür false
.
any
Dizideki herhangi bir bitin 1 olarak ayarlanıp ayarlanmadığını sınar.
bool any() const;
Dönüş Değeri
true
içindeki bitset
herhangi bir bit 1 olarak ayarlanırsa; false
tüm bitler 0 ise.
Örnek
// 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
sınıfının bitset<N>
bir nesnesini oluşturur ve bitleri sıfıra, belirli bir değere ya da dizedeki karakterlerden alınan değerlere başlatır.
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'));
Parametreler
val
Temel iki gösterimi, oluşturulurken bitleri başlatmak için kullanılan işaretsiz tamsayı bitset
.
str
Bit değerlerini başlatmak bitset
için kullanılan sıfırlar ve değerler dizesi.
pos
Dizedeki karakterin soldan sağa sayılarak sıfırla başlayan konumu, içindeki bitset
ilk biti başlatmak için kullanılır.
count
dizesindeki bitler bitset
için ilk değerleri sağlamak için kullanılan karakter sayısı.
Zero
Sıfırı temsil etmek için kullanılan karakter. Varsayılan değer '0'dır.
One
Bir karakteri temsil etmek için kullanılan karakter. Varsayılan değer :'1'.
Açıklamalar
1)
sınıfının bitset<N>
bir nesnesini oluşturur ve tüm N bitleri varsayılan sıfır değerine başlatır.
2-3)
sınıfının bitset<N>
nesnesini oluşturur ve parametresinden val
bitleri başlatır.
4)
sınıfının bitset<N>
nesnesini oluşturur ve sıfır ve bir dizesinde sağlanan karakterlerden bitleri başlatır. Dizenin herhangi bir karakteri 0 veya 1 dışındaysa, oluşturucu sınıfının invalid argument
nesnesini oluşturur. Belirtilen konum (pos
) dizenin uzunluğunu aşıyorsa, oluşturucu sınıfına out_of_range
ait bir nesnesi oluşturur. Oluşturucu yalnızca dizedeki karakterin 1 olduğu j bitset
konumundaki pos + j
bitleri ayarlar. Varsayılan olarak pos
0'dır.
5)
benzer ancak 4)
başka bir parametre içerir, count
bu da başlatacak bit sayısını belirtir. sırasıyla 0 bit ve _One
1 bit anlamına gelen hangi karakterin str
yorumlanması gerektiğini gösteren iki isteğe bağlı parametresi _Zero
vardır.
6)
sıfır ve bir C stili karakter dizesinde sağlanan karakterlere karşılık gelen değerlere N bitlerini başlatan sınıfının bitset<N>
nesnesini oluşturur. Dizeyi bir dize türüne dönüştürmeden oluşturucuyu çağırırsınız, örneğin: bitset<5> b5("01011");
Örnek
// 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
Bit dizisinde ayarlanan bit sayısını döndürür.
size_t count() const;
Dönüş Değeri
Bit dizisinde ayarlanan bit sayısı.
Örnek
// 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
Veri türü için eş anlamlı olan ve içindeki öğe bitlerine başvurmak için kullanılabilen bir bitset
türbool
.
typedef bool element_type;
Örnek
// 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
Bir içindeki tüm bitlerin bitset
değerini ters çevirir veya belirtilen konumda tek bir biti ters çevirir.
bitset<N>& flip();
bitset<N>& flip(size_t pos);
Parametreler
pos
Değeri ters çevrilecek bitin konumu.
Dönüş Değeri
Üye işlevinin çağrıldığı değiştirilmiş bitset
öğesinin bir kopyası.
Açıklamalar
İkinci üye işlevi, parametre olarak belirtilen konum, biti ters çevrilen öğesinin boyutundan N
bitset<N>
büyükse bir out_of_range
özel durum oluşturur.
Örnek
// 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>>;
yok
Nesnede bitset
bit 1 olarak ayarlanmamışsa sınar.
bool none() const;
Dönüş Değeri
true
içinde hiçbir bit bitset
1 olarak ayarlanmamışsa; false
en az bir bit 1 olarak ayarlanmışsa.
Örnek
// 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!=
Belirtilen bit kümesiyle eşitsizlik için hedef bit kümesini test edin.
bool operator!=(const bitset<N>& right) const;
Parametreler
right
Eşitsizlik bitset
için hedef bit kümesiyle karşılaştırılacak olan.
Dönüş Değeri
true
bit kümeleri farklıysa; false
eğer aynıysa.
Açıklamalar
Bit kümeleri aynı boyutta olmalıdır.
Örnek
// 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&=
Bit tabanlı "ve" (&
) işlemiyle bit kümeleri bileşimini yapar.
bitset<N>& operator&=(const bitset<N>& right);
Parametreler
right
bitset
Bit düzeyinde hedef bit kümesiyle birleştirilecek olan.
Dönüş Değeri
Parametre olarak belirtilen bit düzeyinde "ve" (&
) işleminden bitset
kaynaklanan değiştirilen hedef bit kümesi.
Açıklamalar
İşleç tarafından AND
birleştirilen iki bit, her bit doğruysa döndürür true
; aksi takdirde, bunların birleşimi döndürür false
.
İki bit kümesinin aynı boyutta olması gerekir.
Örnek
// 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<<
bir bitset
içindeki bitleri belirtilen sayıda konumu sola kaydırarak sonucu yeni bitset
bir konumuna döndürür.
bitset<N> operator<<(size_t pos) const;
Parametreler
pos
soldaki bitlerin bitset
kaydırılacağı konum sayısı.
Dönüş Değeri
Bitlerin bulunduğu değiştirilen bit kümesi, gerekli konum sayısıyla sola kaydırıldı.
Açıklamalar
Üye işleci işlevi, bir bitset
içindeki bitleri belirtilen sayıda konumu sola kaydırdığı ve sonucu hedeflenen bitset
öğesine döndürdüğü yeri <<=
döndürürbitset(*this) <<= pos
.
Örnek
// 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<<=
bir bitset
içindeki bitleri belirtilen sayıda konumu sola kaydırıp sonucu hedeflenen bitset
konumuna döndürür.
bitset<N>& operator<<=(size_t pos);
Parametreler
pos
soldaki bitlerin bitset
kaydırılacağı konum sayısı.
Dönüş Değeri
Hedeflenen bitset
, bitlerin gerekli konum sayısı kadar sola kaydırıldığı şekilde değiştirildi.
Açıklamalar
Konuma kayacak öğe yoksa, işlev biti 0 değerine temizler.
Örnek
// 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==
Belirtilen bit kümesiyle eşitlik için hedef bit kümesini test edin.
bool operator==(const bitset<N>& right) const;
Parametreler
right
Eşitlik bitset
için hedef bit kümesiyle karşılaştırılacak olan.
Dönüş Değeri
true
bit kümeleri aynıysa; false
eğer farklılarsa.
Açıklamalar
Bit kümeleri aynı boyutta olmalıdır.
Örnek
// 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>>
Bir bitset
içindeki bitleri belirtilen sayıda konumu sağa kaydırarak sonucu yeni bir bit kümesine döndürür.
bitset<N> operator>>(size_t pos) const;
Parametreler
pos
sağdaki bitlerin bitset
kaydırılacağı konum sayısı.
Dönüş Değeri
Bitlerin hedeflenene bitset
göre gerekli konum sayısını sağa kaydırdığı yeni bir bit kümesi.
Örnek
// 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>>=
bir bitset
içindeki bitleri belirtilen sayıda konumu sağa kaydırarak sonucu hedeflenen bitset
öğesine döndürür.
bitset<N>& operator>>=(size_t pos);
Parametreler
pos
sağdaki bitlerin bitset
kaydırılacağı konum sayısı.
Dönüş Değeri
Hedeflenen bitset
, bitlerin gerekli konum sayısı kadar sağa kaydırıldığı şekilde değiştirildi.
Açıklamalar
Konuma kayacak öğe yoksa, işlev biti 0 değerine temizler.
Örnek
// 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[]
değeri değiştirilebilirsebitset
, belirtilen konumdaki bir bitset
bite başvuru döndürür; aksi takdirde, bitin değerini bu konumda döndürür.
bool operator[](size_t pos) const;
reference operator[](size_t pos);
Parametreler
pos
biti içinde bitset
buluan konum.
Açıklamalar
Derlemenizde 1 veya 2 olarak tanımladığınızda _ITERATOR_DEBUG_LEVEL
, sınırlarının bitset
dışındaki bir öğeye erişmeye çalışırsanız yürütülebilir dosyanızda bir çalışma zamanı hatası oluşur. Daha fazla bilgi için bkz . İşaretli Yineleyiciler.
Örnek
// 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^=
Bit tabanlı "xor" (^
) işlemiyle bit kümeleri bileşimini yapar.
bitset<N>& operator^=(const bitset<N>& right);
Parametreler
right
bitset
Bit düzeyinde hedef bit kümesiyle birleştirilecek olan.
Dönüş Değeri
Parametre olarak belirtilen bit düzeyinde "xor" (^
) işleminden bitset
kaynaklanan değiştirilmiş hedef bit kümesi.
Açıklamalar
Bit düzeyinde "xor" işleci (^
) true
ile birleştirilen iki bit, bitlerin true
en az biri olduğunda döndürür, ancak her ikisi birden değilse, bileşimleri döndürür false
.
Bit kümeleri aynı boyutta olmalıdır.
Örnek
// 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|=
Bit tabanlı "veya" (|
) işlemini kullanarak iki bit kümesini birleştirir.
bitset<N>& operator|=(const bitset<N>& right);
Parametreler
right
bitset
Bit düzeyinde hedef bitset
ile birleştirilecek olan .
Dönüş Değeri
Parametre olarak belirtilen bit düzeyinde "veya" (|
) işleminden bitset
kaynaklanan değiştirilmiş hedef bit kümesi.
Açıklamalar
Bitlerden en az biri ise kapsayıcı OR
işleç tarafından birleştirilen iki bit döndürürtrue
; her iki bit true
false
de ise, bunların birleşimi döndürürfalse
.
Bit kümeleri aynı boyutta olmalıdır.
Örnek
// 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~
Hedef bit kümesindeki tüm bitleri ters çevirir ve sonucu döndürür.
bitset<N> operator~() const;
Dönüş Değeri
bitset
hedeflenen ile ilgili olarak tüm bitleri ters çevrilmişbitset
.
Örnek
// 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
sınıfının yardımcı sınıfı olarak tek tek bitlere erişmek ve işlemek için kullanılan bir bitset
içinde yer alan bitlere başvurular sağlayan bir ara sunucu sınıfıbitset
.operator[]
class reference {
friend class bitset<N>;
public:
reference& operator=(bool val);
reference& operator=(const reference& bitref);
bool operator~() const;
operator bool() const;
reference& flip();
};
Parametreler
val
içindeki bir bite atanacak türündeki bool
bitset
nesnenin değeri.
bitref
formun x [ i ]
konumundaki bitine i
bitset
x
başvurusu.
Dönüş Değeri
Sınıf başvurusunun birinci, ikinci ve beşinci üye işlevleri için bağımsız değişken konumu tarafından belirtilen ve veya içindeki bite bitset
yapılan başvuru, true
sınıf başvurusunun üçüncü ve dördüncü üye işlevleri için içinde değiştirilen bitin bitset
değerini yansıtır.false
Açıklamalar
sınıfı reference
yalnızca için bitset
operator[]
bir yardımcı sınıf olarak bulunur. Üye sınıfı, içindeki tek bir bite erişebilen bir bitset
nesneyi açıklar. b
türünde bool
x
bir nesne ve y
türündeki bitset<N>
nesneler ve i
j
bu tür bir nesne içinde geçerli konumlar olsun. Gösterimi x [i]
bit kümesindeki konumundaki i
bite başvurur x
. sınıfının reference
üye işlevleri sırayla aşağıdaki işlemleri sağlar:
İşlem | Tanım |
---|---|
x [i ] = b |
değeri bit kümesinde bit konumunda i depolar bool x .b |
x [i ] = y [j ] |
bit [ j ] değerini bit y kümesinde bit konumunda i depolarx . |
b = ~ x [i ] |
[ i ] bitinin x çevrilmiş değerini içinde bool b depolar. |
b = x [i ] |
[ i ] bitinin x değerini içinde bool b depolar. |
x [i ]. flip ( ) |
[ i ] bitinin x çevrilmiş değerini içinde x bit konumunda i depolar. |
Örnek
// 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
Bir içindeki tüm bitleri 0'a bitset
sıfırlar veya belirli bir konumdaki bir biti 0 olarak sıfırlar.
bitset<N>& reset();
bitset<N>& reset(size_t pos);
Parametreler
pos
0'a sıfırlanacak bitin bitset
konumu.
Dönüş Değeri
Üye işlevinin bitset
çağrıldığı öğesinin bir kopyası.
Açıklamalar
Belirtilen konum boyutunun boyutundan bitset
büyükse, ikinci üye işlevi bir out_of_range
özel durum oluşturur.
Örnek
// 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
Bir bitset
içindeki tüm bitleri 1 olarak ayarlar veya belirli bir konumdaki bir biti 1 olarak ayarlar.
bitset<N>& set();
bitset<N>& set(
size_t pos,
bool val = true);
Parametreler
pos
Atanan değerin ayarlanacağı içindeki bitin bitset
konumu.
val
Belirtilen konumda bite atanacak değer.
Dönüş Değeri
Üye işlevinin bitset
çağrıldığı öğesinin bir kopyası.
Açıklamalar
Belirtilen konum boyutunun boyutundan bitset
büyükse, ikinci üye işlevi bir out_of_range
özel durum oluşturur.
Örnek
// 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
Nesnedeki bitset
bit sayısını döndürür.
size_t size() const;
Dönüş Değeri
bir bitset<N>
içindeki bit N
sayısı.
Örnek
// 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
içinde belirtilen konumdaki bitset
bitin 1 olarak ayarlanıp ayarlanmadığını sınar.
bool test(size_t pos) const;
Parametreler
pos
değerini test etmek için içindeki bitset
bitin konumu.
Dönüş Değeri
true
bağımsız değişken konumu tarafından belirtilen bit 1 olarak ayarlanırsa; aksi takdirde , false
.
Açıklamalar
Üye işlevi bir out_of_range
to_string
Nesneyi dize bitset
gösterimine dönüştürür.
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;
Dönüş değeri
sınıfının basic_string
dize nesnesi; burada içindeki bitset
her bit kümesinde karşılık gelen 1 karakteri ve bit ayarlanmamışsa 0 karakteri bulunur.
Örnek
// 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
Nesnenin içeriğiyle bitset
aynı bit kümesini içeren bir unsigned long long
değer döndürür.
unsigned long long to_ullong() const;
Dönüş değeri
Bit dizisindeki bit değerlerinin toplamını olarak unsigned long long
döndürür. Bu unsigned long long
değer, bir bitset
başlatmak için kullanılırsa aynı küme bitlerini yeniden oluşturur.
Özel durumlar
bit dizisindeki herhangi bir overflow_error
bit, türünde unsigned long long
bir değer olarak temsil edilmeyecek bir bit değerine sahipse bir nesnesi oluşturur.
Açıklamalar
Bit dizisindeki bit değerlerinin toplamını olarak unsigned long long
döndürür.
to_ulong
Bir bitset
nesneyi, başlatmak için kullanılırsa bulunan bit dizisini oluşturacak tamsayıya bitset
dönüştürür.
unsigned long to_ulong( ) const;
Dönüş değeri
öğesinin başlatılmasında bitset
kullanılırsa içinde bitleri oluşturacak bir bitset
tamsayı.
Açıklamalar
Üye işlevinin uygulanması, içinde bulunan bit dizisinde bulunan 1 ve 0 basamaklık aynı diziye sahip tamsayıyı bitset
döndürür.
Üye işlevi, bit dizisindeki herhangi bir bit türünde bir overflow_error
değer olarak temsil edilmeyecek bir bit değerine unsigned long
sahipse bir nesnesi oluşturur.
Örnek
// 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.