Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Between std::bitset and std::vector<bool>

I have a std::bitset but now I want to use an STL algorithm on it.

I could have used std::vector<bool> instead, but I like std::bitset's constructor and I want std::bitset's bitwise operations.

Do I have to go through a loop and stuff everything in a std::vector<bool> to use STL algorithms, and then copy that back to the std::bitset, or is there a better way?

like image 420
Jonathan Mee Avatar asked Jan 07 '15 18:01

Jonathan Mee


1 Answers

If you do not want to write loops using the operator[] of the bitset, then you might try using bitset::to_string() to convert the bitset to a string of '1' and '0'. Since C++11, you can actually choose different characters than those two, so you could actually choose '\0' and '\1'.

Are you sure bitset is the optimal type for your task?

like image 136
wilx Avatar answered Nov 15 '22 22:11

wilx