기본적으로 C ++ 11에서 부울 벡터를 대체하는 비트 마스크 벡터를 어떻게 생성합니까?
다른 벡터의 특정 인덱스 값을 인쇄해야하는지 여부를 알려주는 참 / 거짓 값을 저장하기 위해 비트 마스크 벡터를 만들려고합니다.
예 : std::vector<int> a ;
b는 정수 벡터 a에 해당하는 플래그 값을 보유 할 비트 마스크의 벡터이며이 플래그는 해당 인덱스의 특정 값을 인쇄해야하는지 여부를 알려줍니다.
a {1,2,3}
b { true, false ,true} // I need a similar bitmask which would help me print 1 and 3
이 작업은 내가 작업중인 문제에 비트 마스크를 사용해야하는 다른 방법으로 수행 할 수 있습니다. 도와 주셔서 미리 감사드립니다.
답변
Pat.ANDRIA
많은 사람들이 이미 제안했듯이 다음과 같이했을 것입니다.
myBitset
깃발을 유지합니다.1
(set) 인쇄 플래그 용 및0
(clear) 비 인쇄 플래그 용
#include <bitset>
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char** argv){
std::vector<int> a {1,2,3,4,5,6,7,8,9,10};
std::bitset<10> myBitset;
myBitset.set(3); // set fourth bit ==> display 4
myBitset.set(6); // set seventh bit ==> display 7
myBitset[8] = true; // set nineth bit ==> display 9
myBitset[9] = myBitset[3]; // set 10th bit ==> display 10
std::cout << "Mybitset" << myBitset << endl;
for (int i=0; i<a.size(); i++)
{
if (myBitset.test(i))
{
std::cout << a.at(i);
}
}
return (0);
}
출력은 다음과 같습니다.
1101001000
47910