我有一个 bool vector 。我需要将其元素从第 n 次到第 m 次设置为 true
.有没有不使用循环的优雅方法?
编辑:感谢所有指出使用 vector<bool>
问题的人.不过,我一直在寻找更通用的解决方案,比如 jalf 给出的解决方案。
最佳答案
algorithm
header 中的
std::fill
或 std::fill_n
应该可以解决问题。
// 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);
关于c++ - 如何将 STL vector 中的一系列元素设置为特定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1138724/