newbie
newbie

Reputation: 4789

Creating class dynamically

I need some suggestion. I am reading a file which has input like this

001 100 110 000 ... ..

These are consecutive input and output pairs.

Now I need to read these 1 and 0 for further processing. I need to have an array like input[0]= {0,0,1} input[1] = {1,1,0} ....for all the inputs. I will do bitwise compare, xor, or.. etc with the input and output pair. I was thinking about creating a class so that after reading the input/outputs I can create objects of that class where all the required functions will be overloaded. As an example, this case the object will be an array of 3 elements and will consist of only 1 and 0.

Now the problem is, the number of 1 and 0s can vary. Is it possible to create a class where the member will be an undefined sized array? Or is there any other way around?

I am new in this. So, I would appreciate to get some help.

Upvotes: 1

Views: 180

Answers (2)

Jesse Good
Jesse Good

Reputation: 52365

Use std::bitset, std::string and std::vector.

std::string bits = "001";
std::bitset<3> b(bits);

std::bitset already provides bitwise operations for you.

The create an std::vector<std::bitset<3>> of them and work with that.

Upvotes: 2

Qaz
Qaz

Reputation: 61910

Make your data member a std::vector. You can use push_back to add elements onto the end, and a whole other whack of stuff. Elements can be accessed just like an array. There are lots of good references on it out there.

Even better, just use vectors in the first place and overload the operators to take two vectors.

Upvotes: 1

Related Questions