當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。