unordered_multiset cbegin()函数的C++ STL
unordered_multiset::cbegin() 是C++ STL中的内置函数,它返回指向容器中第一个元素或其中一个桶中第一个元素的常量迭代器。
语法:
unordered_multiset_name.cbegin(n)
参数: 该函数接受一个参数。如果传递了参数,则返回指向桶中第一个元素的常量迭代器。如果没有传递参数,则返回指向unordered_multiset容器中第一个元素的常量迭代器。
返回值: 它返回一个常量迭代器。它不能用于更改unordered_multiset元素的值。
下面的程序说明了上述函数:
程序1:
// C++ program to illustrate the
// unordered_multiset::cbegin() 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);
// prints all element
cout << "Elements: ";
for (auto it = sample.cbegin(); it != sample.cend(); it++)
cout << *it << " ";
auto it = sample.cbegin();
// print the first element
cout << "\nThe first element: " << *it;
return 0;
}
程序2:
// C++ program to illustrate the
// unordered_multiset::cbegin() 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.cbegin();
cout << "The first element: " << *it;
it++;
cout << "\nThe second element: " << *it;
cout << "\nElements: ";
// prints all element
for (auto it = sample.cbegin(); it != sample.cend(); it++)
cout << *it << " ";
return 0;
}
程序3:
// C++ program to illustrate the
// unordered_multiset::cbegin() 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.cbegin(1);
cout << "\nElements in first bucket: ";
// prints all element
for (auto it = sample.cbegin(1); it != sample.cend(1); it++)
cout << *it << " ";
return 0;
}