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

📅  最后修改于: 2021-05-30 13:40:28             🧑  作者: Mango

unordered_multimap :: cbegin()是C++ STL中的内置函数,该函数返回一个常量迭代器,该迭代器指向容器中的第一个元素或其存储桶中的第一个元素。

句法:

unordered_multimap_name.cbegin(n)

参数:该函数接受一个参数。如果传递了参数,它将返回一个常量迭代器,该迭代器指向存储桶中的第一个元素。如果未传递任何参数,则它将返回一个常量迭代器,该迭代器指向unordered_multimap容器中的第一个元素。

返回值:它返回一个常量迭代器。它不能用于更改unordered_multimap元素的值。

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

程序1:

// C++ program to illustrate the
// unordered_multimap::cbegin() 
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap sample;
  
    // inserts key and element
    sample.insert({ 1, 2 });
    sample.insert({ 3, 4 });
    sample.insert({ 3, 4 });
    sample.insert({ 2, 3 });
    sample.insert({ 2, 3 });
  
    // prints all key and element
    cout << "Key and Elements : \n";
    for (auto it = sample.begin(); it != sample.end(); it++)
        cout << "   " << it->first << "\t      " 
             << it->second << endl;
  
    auto it = sample.begin();
  
    // print the first element
    cout << "\nThe first element and key: " 
         << it->first << " ";
    cout << it->second;
  
    return 0;
}
输出:
Key and Elements : 
   2          3
   2          3
   1          2
   3          4
   3          4

The first element and key: 2 3

程式2:

// C++ program to illustrate the
// unordered_multimap::cbegin(bucket) 
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap sample;
  
    // inserts element
    sample.insert({ 1, 2 });
    sample.insert({ 3, 4 });
    sample.insert({ 3, 4 });
    sample.insert({ 2, 3 });
    sample.insert({ 2, 3 });
  
    // prints all element
    cout << "Key and Elements of first bucket: \n";
    for (auto it = sample.cbegin(1); it != sample.cend(1); it++)
        cout << "   " << it->first << "\t      " 
             << it->second << endl;
  
    auto it = sample.cbegin(1);
  
    // print the first element
    cout << "\nThe first element and key in first bucket: "
         << it->first << " ";
    cout << it->second;
  
    return 0;
}
输出:
Key and Elements of first bucket: 
   1          2

The first element and key in first bucket: 1 2
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”