📜  C++ STL中的unordered_multiset begin()函数

📅  最后修改于: 2021-05-30 10:44:24             🧑  作者: Mango

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

句法:

unordered_multiset_name.begin(n)

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

返回值:返回一个迭代器。

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

程序1:

// C++ program to illustrate the
// unordered_multiset::begin() function
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset sample;
  
    // inserts element
    sample.insert(10);
    sample.insert(11);
    sample.insert(15);
    sample.insert(13);
    sample.insert(14);
  
    // print the first element
    cout << "The first element: " << *sample.begin();
  
    cout << "\nElements: ";
  
    // prints all element
    for (auto it = sample.begin(); it != sample.end(); it++)
        cout << *it << " ";
    return 0;
}
输出:
The first element: 14
Elements: 14 13 15 10 11

程式2:

// C++ program to illustrate the
// unordered_multiset::begin() function
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset sample;
  
    // inserts element
    sample.insert('a');
    sample.insert('b');
    sample.insert('c');
    sample.insert('x');
    sample.insert('z');
  
    // print the first element
  
    auto it = sample.begin();
    cout << "The first element: " << *it;
  
    it++;
    cout << "\nThe second element: " << *it;
  
    cout << "\nElements: ";
  
    // prints all element
    for (auto it = sample.begin(); it != sample.end(); it++)
        cout << *it << " ";
    return 0;
}
输出:
The first element: z
The second element: x
Elements: z x c a b

程序3:

// C++ program to illustrate the
// unordered_multiset::begin() function
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset sample;
  
    // inserts element
    sample.insert('a');
    sample.insert('b');
    sample.insert('c');
    sample.insert('x');
    sample.insert('z');
  
    // print the first element
    cout << "The first element in first bucket : " << *sample.begin(1);
  
    cout << "\nElements in first bucket: ";
  
    // prints all element
    for (auto it = sample.begin(1); it != sample.end(1); it++)
        cout << *it << " ";
    return 0;
}
输出:
The first element in first bucket : x
Elements in first bucket: x c
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”