简体   繁体   中英

Convert Array of Bitset to vector<bool>

I have set of zeroes and ones as my input like below, I need to do some pairwise Boolean operation (and, or, Xor, not) between them (consider each line as).

111100000000
101100000010
111011100000
111100000001
001100010001

The code for reading and storing each line is:

int lineCounter = 0;
while (std::getline(infile, line))
{
    myinput[lineCounter] = bitset<LEN> (std::string(line));
    lineCounter++;
}

Right now I am using array of bitset to store each line bitset<LEN> myinput[NUMBER]; that LEN is size of each line and NUMBER is number of lines in my input file. But the problem is I don't want to specify LEN and NUMBER during compile time since I have to work with different input. having said that I want user give the LEN and NUMBER as an input argument when running the program. Since I can not do dynamic allocation for bitset I want to use vector but don't know how should I use it to fulfill my job! can you please tell how can read and store my input and do pairwise boolean operation with help of vector or anything else that can handle dynamic allocation.

You can read the input like this:

vector< vector<bool> > set;
int lineCounter = 0;
while (std::getline(infile, line))
{
         string input = string(line)
   vector<bool> line;
   while(input.size!=0){
        if(input.front()=='0'{
            line.pushBack(false);
        }
        else{
            line.pushBack(true);
        }
    input.erase(0,1);
    }
    set.pushback(line);
    lineCounter++;
}

As far as pairwise boolean operations, iterating over the two vectors and performing the appropriate bitwise opperation should suffice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM