std::count
时间: 2023-08-19 09:11:19 浏览: 66
std::count是C++标准库中的一个算法函数,用于计算指定范围内的元素个数。它接受三个参数:范围的起始迭代器、范围的结束迭代器和要计数的元素值。函数会返回范围内等于指定元素值的元素个数。\[1\]
例如,下面的代码片段演示了如何使用std::count函数来计算一个vector中等于指定值的元素个数:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
int nCount = std::count(v.begin(), v.end(), 3);
std::cout << "容器中元素为3的元素数量:" << nCount << std::endl;
//输出:容器中元素为3的元素数量:2
return 0;
}
```
在上述代码中,我们使用std::count函数计算了vector v中等于3的元素个数,并将结果存储在变量nCount中。最后,我们将结果输出到控制台。\[2\]
除了计算等于指定值的元素个数,std::count还可以通过使用lambda表达式来计算满足特定条件的元素个数。例如,下面的代码片段演示了如何使用std::count_if函数来计算一个vector中能被3整除的元素个数:
```cpp
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
int nCount = std::count_if(v.begin(), v.end(), \[\](int i) {return i % 3 == 0; });
std::cout << "容器中能被3整除的元素数量: " << nCount << std::endl;
//输出:容器中能被3整除的元素数量: 3
return 0;
}
```
在上述代码中,我们使用std::count_if函数和lambda表达式来计算vector v中能被3整除的元素个数,并将结果存储在变量nCount中。最后,我们将结果输出到控制台。\[2\]
总结起来,std::count是一个非常有用的算法函数,可以用于计算指定范围内满足特定条件的元素个数。它可以通过指定元素值或使用lambda表达式来进行计数。\[1\]\[2\]
#### 引用[.reference_title]
- *1* *3* [c++判断vector中是否存在特定元素的方法](https://blog.csdn.net/guotianqing/article/details/105832070)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [std::count、std::count_if 用法](https://blog.csdn.net/phd17621680432/article/details/122562641)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)