Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating vector of boost dynamic_bitset in C++

Tags:

c++

boost

I want to create an array of dynamic_bitsets. So I created a vector of dynamic_bitset using,

vector<boost::dynamic_bitset<>> v;

How can I specify the size of each of these dynamic_bitsets i.e. v[0], v[1] etc? Like in a general case, we specify the size through the constructor.

boost::dynamic_bitset<> x(3);
like image 226
SyncMaster Avatar asked Jul 06 '12 23:07

SyncMaster


1 Answers

This line

vector<boost::dynamic_bitset<>> v;

create an empty vector. Instead you could have requested it be filled with default entries which all have the same value, so like one usually does

vector<int> v(N, 1);

to create a vector with N entries all 1 you could do

vector<boost::dynamic_bitset<>> v( N, boost::dynamic_bitset<>(3) ) ;

to have it contain N boost::dynamic_bitset<>s with 3 bits.

If your vector contains enough elements you should be able to set the v[i] to a different size

v[i] = boost::dynamic_bitset<>( 100 ) ;

Alternative you could create an empty vector like you currently do and just use something like v.push_back(boost::dynamic_bitset<>(42)) to add correctly sized elements.

like image 125
Benjamin Bannier Avatar answered Oct 02 '22 11:10

Benjamin Bannier