0

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.

Am1rr3zA
  • 7,115
  • 18
  • 83
  • 125
  • 2
    Boost `dynamic_bitset` ? [The `dynamic_bitset` class is nearly identical to the `std::bitset` class. The difference is that the size of the `dynamic_bitset` (the number of bits) is specified at run-time during the construction of a `dynamic_bitset` object, whereas the size of a `std::bitset` is specified at compile-time through an integer template parameter.](http://www.boost.org/doc/libs/1_57_0/libs/dynamic_bitset/dynamic_bitset.html) – Ben Voigt Feb 03 '15 at 22:23

1 Answers1

3

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.

chessprogrammer
  • 768
  • 4
  • 15