C++vector中0的个数
时间: 2024-03-07 21:48:02 浏览: 80
c++从0开始
您好!可以通过遍历vector中的元素,统计0的个数。以下是一个示例代码:
```c++
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = {1, 0, 3, 0, 5, 0};
int count = 0;
for (int i = 0; i < v.size(); i++) {
if (v[i] == 0) {
count++;
}
}
cout << "The number of 0 in the vector is " << count << endl;
return 0;
}
```
输出结果为:
```
The number of 0 in the vector is 3
```
希望能够帮到您!
阅读全文