count函数
语法:
1 |
size_type count(const key_type& __x) const; |
count()返回集合中某个值元素的个数
源代码
1 2 3 4 5 6 7 8 9 10 11 |
/** * @brief Finds the number of elements. * @param x Element to located. * @return Number of elements with specified key. * * This function only makes sense for multisets; for set the result will * either be 0 (not present) or 1 (present). */ size_type count(const key_type& __x) const { return _M_t.find(__x) == _M_t.end() ? 0 : 1; } |
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// 梁笔记 // https://zouzhongliang.com #include <iostream> #include <set> using namespace std; int main() { set<int> s1; s1.insert(10); s1.insert(12); s1.insert(13); s1.insert(9); cout<<"s1集合中元素数量:"<<s1.size()<<endl; cout<<"s1集合中15元素的个数"<<s1.count(15)<<endl; cout<<"s1集合中10元素的个数:"<<s1.count(10)<<endl; } |
测试结果:
1 2 3 |
s1集合中元素数量:4 s1集合中15元素的个数0 s1集合中10元素的个数:1 |