在C++ STL中使用unordered_multiset begin()函数
unordered_multiset::begin() 是C++ STL中的一个内置函数,它返回一个迭代器,指向容器中的第一个元素或其中一个桶中的第一个元素。
语法:
unordered_multiset_name.begin(n)
参数: 该函数接受一个参数。如果传递了参数,则返回一个指向桶中第一个元素的迭代器。如果没有传递参数,则返回一个指向unordered_multiset容器中第一个元素的迭代器。
返回值: 该函数返回一个迭代器。
下面的程序说明了上面的函数:
程序 1:
// C++ program to illustrate the
// unordered_multiset::begin() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaration
unordered_multiset<int> 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 <bits/stdc++.h>
using namespace std;
int main()
{
// declaration
unordered_multiset<char> 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 <bits/stdc++.h>
using namespace std;
int main()
{
// declaration
unordered_multiset<char> 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