例如:
b = [TTFFTF],则函数返回 3 。
我可以通过 for 循环程序来做到这一点。但是,是否有函数返回“布尔向量中真值的数量”来比 for 循环程序更快地做到这一点?
谢谢。
als*_*her 10
std::count(b.begin(), b.end(), true);
可能是个好主意。如果你想尝试更详细:
#include <vector> // std::vector
#include <algorithm> // std::count
#include <iostream> // std::cout, std::endl
int main(){
std::vector<bool> b = { true, true, false, true, false, true };
auto count = std::count(b.begin(), b.end(), true);
std::cout << "Count = " << count << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
计数 = 4
| 归档时间: |
|
| 查看次数: |
2525 次 |
| 最近记录: |