admin管理员组

文章数量:1794759

C++ Map中Count函数的使用

C++ Map中Count函数的使用

学习参考【wwwblogs/Deribs4/p/4948351.html】

【blog.csdn/YinJianxiang/article/details/76114505】algorithm头文件定义了一个Count的函数,其功能类似于find。这个函数使用一对迭代器和一个值做参数,返回值是当前容器中,所查找元素存在的个数(对于Map来说,返回值为1或者0)。

 

count_if函数

count_if :返回区间中满足指定条件的元素数目。 

#include <vector> #include <algorithm> #include <iostream> bool greater10(int value) { return value >10; } int main() { using namespace std; vector<int> v1; vector<int>::iterator Iter; v1.push_back(10); v1.push_back(20); v1.push_back(10); v1.push_back(40); v1.push_back(10); cout << "v1 : "; for (Iter = v1.begin(); Iter != v1.end(); Iter++) cout << *Iter << " "; cout << endl; vector<int>::size_type result1 = count_if(v1.begin(), v1.end(), greater10); //count_if算法返回使谓词函数返回条件成立的元素个数 cout << "The number of elements in v1 greater than 10 is: " << result1 << "." << endl; return 0; }

 

本文标签: 函数MapCount