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


C++ unordered_multimap size()用法及代码示例


unordered_multimap::size()是C++ STL中的内置函数,该函数返回unordered_multimap的大小。它表示该容器中元素的数量。

用法:

unordered_multimap_name.size()

参数:该函数不接受任何参数。


返回值:它返回一个整数值,该值表示容器的大小。

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

示例1:

// C++ program to illustrate the 
// unordered_multimap::size() 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multimap<int, int> sample1, sample2; 
  
    // inserts key and element 
    // in sample1 
    sample1.insert({ 10, 100 }); 
    sample1.insert({ 50, 500 }); 
  
    // inserts key and element 
    // in sample1 
    sample2.insert({ 20, 200 }); 
    sample2.insert({ 30, 300 }); 
    sample2.insert({ 30, 150 }); 
  
    cout << "The size of Sample1 is: " << sample1.size(); 
  
    cout << "\nKey and Elements of Sample1 are:"; 
    for (auto it = sample1.begin(); it != sample1.end(); it++) { 
        cout << "{" << it->first << ", " << it->second << "} "; 
    } 
  
    cout << "\n\nThe size of Sample2 is: " << sample2.size(); 
  
    cout << "\nKey and Elements of Sample2 are:"; 
    for (auto it = sample2.begin(); it != sample2.end(); it++) { 
        cout << "{" << it->first << ", " << it->second << "} "; 
    } 
  
    return 0; 
}
输出:
The size of Sample1 is: 2
Key and Elements of Sample1 are:{50, 500} {10, 100} 

The size of Sample2 is: 3
Key and Elements of Sample2 are:{20, 200} {30, 150} {30, 300}

示例2:

// C++ program to illustrate the 
// unordered_multimap::size() 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multimap<char, char> sample1, sample2; 
  
    // inserts key and element 
    // in sample1 
    sample1.insert({ 'a', 'A' }); 
    sample1.insert({ 'g', 'G' }); 
  
    // inserts key and element 
    // in sample1 
    sample2.insert({ 'b', 'B' }); 
    sample2.insert({ 'c', 'C' }); 
    sample2.insert({ 'd', 'D' }); 
  
    cout << "The size of Sample1 is: " << sample1.size(); 
  
    cout << "\nKey and Elements of Sample1 are:"; 
    for (auto it = sample1.begin(); it != sample1.end(); it++) { 
        cout << "{" << it->first << ", " << it->second << "} "; 
    } 
  
    cout << "\n\nThe size of Sample2 is: " << sample2.size(); 
  
    cout << "\nKey and Elements of Sample2 are:"; 
    for (auto it = sample2.begin(); it != sample2.end(); it++) { 
        cout << "{" << it->first << ", " << it->second << "} "; 
    } 
  
    return 0; 
}
输出:
The size of Sample1 is: 2
Key and Elements of Sample1 are:{g, G} {a, A} 

The size of Sample2 is: 3
Key and Elements of Sample2 are:{d, D} {b, B} {c, C}


相关用法


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