当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ unordered_multiset count()用法及代码示例


unordered_multiset::count()是C++ STL中的内置函数,该函数返回unordered_multiset容器中等于给定值的元素计数。

用法:

unordered_multiset_name.count(val)

参数:该函数接受单个强制参数val,该参数指定要返回其元素在unordered_multiset容器中的计数的元素。


返回值:它返回一个无符号整数类型,该整数类型表示一个值在容器中出现的次数。

以下示例程序旨在说明上述函数:

示例1:

// C++ program to illustrate the 
// unordered_multiset::count() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multiset<int> sample; 
  
    // inserts element 
    sample.insert(11); 
    sample.insert(11); 
    sample.insert(11); 
    sample.insert(12); 
    sample.insert(13); 
    sample.insert(13); 
    sample.insert(14); 
  
    cout << "11 occurs " << sample.count(11) << " times"; 
    cout << "\n12 occurs " << sample.count(13) << " times"; 
    cout << "\n13 occurs " << sample.count(13) << " times"; 
    cout << "\n14 occurs " << sample.count(14) << " times"; 
  
    return 0; 
}
输出:
11 occurs 3 times
12 occurs 2 times
13 occurs 2 times
14 occurs 1 times

示例2:

// C++ program to illustrate the 
// unordered_multiset::count() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multiset<char> sample; 
  
    // inserts element 
    sample.insert('a'); 
    sample.insert('a'); 
    sample.insert('a'); 
    sample.insert('b'); 
    sample.insert('b'); 
    sample.insert('c'); 
    sample.insert('c'); 
  
    cout << "a occurs " << sample.count('a') << " times"; 
    cout << "\nb occurs " << sample.count('b') << " times"; 
    cout << "\nc occurs " << sample.count('c') << " times"; 
  
    return 0; 
}
输出:
a occurs 3 times
b occurs 2 times
c occurs 2 times


相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 unordered_multiset count() function in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。