I have a vector of booleans. I need to set its elements from n-th to m-th to true
. Is there an elegant way to do this without using a loop?
Edit: Tanks to all those who pointed out the problems with using vector<bool>
. However, I was looking for a more general solution, like the one given by jalf.
std::fill
or std::fill_n
in the algorithm
header should do the trick.
// set m elements, starting from myvec.begin() + n to true
std::fill_n(myvec.begin() + n, m, true);
// set all elements between myvec.begin() + n and myvec.begin() + n + m to true
std::fill(myvec.begin() + n, myvec.begin() + n + m, true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With