📜  C++ STL中的unordered_multimap size()函数

📅  最后修改于: 2021-05-30 14:26:47             🧑  作者: Mango

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

句法:

unordered_multimap_name.size()

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

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

下面的程序说明了上述函数:

程序1:

// C++ program to illustrate the
// unordered_multimap::size()
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap 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 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap 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}
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”