bitset은 bool의 배열이지만 각 부울 값은 대신 별도의 바이트에 저장되지 않습니다. bitset은 다음과 같이 공간을 최적화합니다. 각 부울 값은 1비트 공간만 차지합니다. , 그래서 bitset이 차지하는 공간은 bool 배열 또는 bool 벡터의 공간보다 작습니다. .
비트셋의 한계는 다음과 같습니다. 크기는 컴파일 타임에 알려야 합니다. 즉, 비트 세트의 크기가 고정되어 있습니다.
표준::비트세트 내부에 정의된 bitset의 클래스 템플릿입니다. 헤더 파일 따라서 프로그램에서 bitset을 사용하기 전에 헤더 파일을 포함해야 합니다.
알파벳에 번호를 매기다
통사론:
bitset variable_name(initialization);>
세 가지 방법으로 비트세트를 초기화할 수 있습니다.
1. 초기화되지 않음: 모든 비트는 0으로 설정됩니다.
bitset variable_name;>
2. 십진수로 초기화: Bitset은 주어진 십진수를 이진 형식으로 나타냅니다.
bitset variable_name(DECIMAL_NUMBER);>
3. 바이너리 문자열로 초기화: Bitset은 주어진 이진 문자열을 나타냅니다.
bitset variable_name(string('BINARY_STRING')); bitset variable_name('BINARY_STRING');>예:
C++ // C++ program to demonstrate the bitset #include #include using namespace std; int main() { // declaring an uninitialized bitset object bitset<8>초기화되지 않은Bitset; // 10진수 비트셋으로 초기화<8>십진수비트세트(15); // 바이너리 문자열 비트셋으로 초기화<8>stringBitset(string('1111')); 시합<< 'Uninitialized bitset: ' << uninitializedBitset << endl; cout << 'Initialized with decimal: ' << decimalBitset << endl; cout << 'Initialized with string: ' << stringBitset << endl; return 0; }> 산출
Uninitialized bitset: 00000000 Initialized with decimal: 00001111 Initialized with string: 00001111>
std::bitset 멤버 함수
std::bitset 클래스에는 bitset 개체에 대해 작업하는 데 유용한 몇 가지 멤버 함수가 포함되어 있습니다. 다음은 std::bitset의 일부 멤버 함수 목록입니다.
기능 이름 | 기능 설명 |
|---|---|
세트() | 설정 비트 값 주어진 인덱스에서 1로 . |
초기화() | 설정 비트 값 특정 인덱스에서 0으로 . |
튀기다() | 비트 값 반전 주어진 인덱스에서. |
세다() str을 int로 변환하는 방법 | 세다 개수 비트 설정 . |
시험() | 다음을 반환합니다. 부울 값 주어진 인덱스에서 . |
어느() | 다음 사항을 확인합니다. 어느 비트는 세트 . |
없음() | 다음 사항을 확인합니다. 없음 비트는 세트. |
모두() | 다음 사항을 확인하세요. 모두 비트는 세트. |
크기() | 다음을 반환합니다. 비트셋의 크기. |
to_string() | 개종하다 비트셋을 std::string으로 설정합니다. |
to_follow() | 개종하다 unsigned long으로 비트셋을 설정합니다. |
to_ullong() | 개종하다 unsigned long long으로 비트셋을 설정합니다. |
예:
C++ // C++ program to demonstrate the // use of std::bitset member // functions #include #include using namespace std; int main() { // declaring index variable int index = 0; // declaring few bitset objects bitset<4>allSet('1111'), allUnset; 시합<< 'any() value: ' << boolalpha << allSet.any() << endl; cout << 'all() value: ' << allSet.all() << endl; cout << 'none() value: ' << allSet.none() << endl; cout << 'test() at index 0: ' << noboolalpha << allSet.test(index) << endl; cout << 'size() value: ' << allSet.size() << endl; cout << 'Value of allUnset on before using set(): ' << allUnset << endl; allUnset.set(index); cout << 'Value of allUnset on after using set(): ' << allUnset << endl; cout << 'Value of allSet on before using reset(): ' << allSet << endl; allSet.reset(index); cout << 'Value of allSet on after using reset(): ' << allSet << endl; // declaring an empty string string bitString; // using to_string() method to assign value to empty // string bitString = allSet.to_string(); cout << 'bitString: ' << bitString << endl; cout << 'Unsigned Long value: ' << allSet.to_ulong(); return 0; }> 산출
any() value: true all() value: true none() value: false test() at index 0: 1 size() value: 4 Value of allUnset on before using set(): 0000 Value of allUnset on after using set(): 0001 Value of allSet on before using reset(): 1111 Value of allSet on after using reset(): 1110 bitString: 1110 Unsigned Long value: 14>
메모: boolalpha는 부울 값의 경우 1 또는 0 대신 true 및 false를 인쇄하고 반대의 경우 noboolalpha를 인쇄하는 데 사용됩니다.
std::bitset 연산자
일부 기본 연산자는 비트 세트 개체를 사용하기 위해 오버로드됩니다. 다음은 해당 연산자의 목록입니다.
운영자 | 작업 |
|---|---|
[] | 입장 운영자 |
& | 비트별 그리고 |
| | 비트별 또는 |
! | 비트별 무료 |
>>= | 바이너리 오른쪽 시프트 할당하고 |
<<= | 바이너리 왼쪽 시프트 할당하고 |
&= | 비트별 값을 할당합니다. 그리고 첫 번째 비트세트로 이동합니다. |
|= | 비트별 값을 할당합니다. 또는 첫 번째 비트세트로 이동합니다. |
^= | 비트별 값을 할당합니다. 무료 첫 번째 비트세트로 이동합니다. |
~ | 비트별 아니다 |
예:
C++ // C++ program to show the different operator functions on // bitset #include #include using namespace std; int main() = bitset2) << endl; // bitwise NOT cout << 'Bitwise NOT: ' << (result = ~bitset1) << endl; // bitwise XOR bitset1 = 9; cout << 'Bitwise XOR: ' << (bitset1 ^= bitset2) << endl; bitset1 = 9; cout << 'Binary leftshift on bitwise1: ' << (bitset1 <<= 1) << endl; bitset1 = 9; cout << 'Binary rightshift on bitwise1: ' << (bitset1>>= 1)<< endl; return 0;>
산출
Bitset1: 1001 Bitset2: 1010 Accessing bit value at index 1 of bitset1: 0 Bitwise AND using &: 1000 Bitwise AND using &=: 1000 Bitwise OR using |: 1011 Bitwise OR using |=: 1011 Bitwise NOT: 0100 Bitwise XOR: 0011 Binary leftshift on bitwise1: 0010 Binary rightshift on bitwise1: 0100>
std::bitset과 std::Vector와 bool 배열의 차이점
bool 벡터와 bool 배열을 구현하여 bitset과 같은 일련의 부울 값을 저장할 수도 있지만 각 구현 간에는 몇 가지 차이점이 있습니다.
| 매개변수 | 비트셋 라텍스의 부분 차별화 | bool의 벡터 | 부울의 배열 |
|---|---|---|---|
| 정의 | 각 비트가 1비트의 메모리를 차지하도록 저장된 비트 시퀀스로 구성된 클래스 템플릿입니다. | 각 요소의 크기가 1비트이고 유형이 bool인 C++ STL 벡터의 변형입니다. | bool 데이터 요소의 고정 크기 연속 컬렉션입니다. |
| 크기 | 고정 크기. | 동적 크기. | 고정 크기. |
| 메모리 | 단일 요소는 1비트의 메모리를 차지합니다. | 단일 요소는 1비트의 메모리를 차지합니다. | 단일 요소는 1바이트의 메모리를 차지합니다. |
| 속도 | 같은 | 같은 | 더 빠르게 |