在C ++ STL中的unordered_multimap begin()和end()函数
unordered_multimap :: begin()是C++ STL中的内置函数,它返回一个迭代器,指向容器中的第一个元素或其桶中的第一个元素。
语法:
unordered_multimap_name_.begin(n)
参数: 该函数接受一个参数。如果传递了一个参数,则返回指向桶中第一个元素的迭代器。如果未传递参数,则返回指向unordered_multimap容器中第一个元素的常量迭代器。
返回值: 它返回一个迭代器。它不能用于更改unordered_multimap元素的值。
以下是说明上述函数的程序:
程序1:
// C++ program to illustrate the
// unordered_multimap::begin() function
#include& lt; bits / stdc++.h & gt;
using namespace std;
int main()
{
    // declaration
    unordered_multimap& lt;
    int, int& gt;
    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& lt;
    <
    "
    Key and Elements : \n& quot;
    ;
    for (auto it = sample.begin(); it != sample.end(); it++)
        cout& lt;
    <
    "
    "
    <
    <
    it - >
    first& lt;
    <
    "
    \t& quot;
    <
    <
    it - >
    second& lt;
    <
    endl;
    auto it = sample.begin();
    // print the first element
    cout& lt;
    <
    "
    \nThe first key and element : "
    <
    <
    it - >
    first& lt;
    <
    "
    "
    ;
    cout& lt;
    <
    it - >
    second;
    return 0;
}
输出:
Key and Elements: 
   2          3
   2          3
   1          2
   3          4
   3          4
The first key and element: 2 3
程序2:
// C ++ program to illustrate the
// unordered_multimap :: begin(bucket) function
#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 << "\nKey and Elements of first bucket : \n";
    for (auto it = sample.begin(1); it != sample.end(1);
            it++)
        cout << "\t" << it->first <<
                "\t" << it->second << endl;
    auto it = sample.begin(1);
    // print the first element
    cout << "\nThe first key and element in first bucket : "
        << it->first
        << "\t" << it->second;
    return 0;
}
输出:
Key and Elements of first bucket: 
   1          2
The first key and element in first bucket: 1 2
unordered_multimap :: end() 是C++ STL中的内置函数,它返回一个迭代器,指向容器中最后一个元素之后的位置或其桶中最后一个元素之后的位置。
语法:
unordered_multimap_name_.end(n)
参数: 该函数接受一个参数。如果传递了参数,则返回一个迭代器,指向其一个bucket中最后一个元素的后面的位置。如果没有传递参数,则返回一个迭代器,指向unordered_multimap容器的最后一个元素的后面位置。
返回值: 返回一个迭代器。它不能用于更改unordered_multimap元素的值。
下面是说明以上函数的程序:
程序1:
// C++ program to illustrate the
// unordered_multimap::end() function
 
#include& lt; bits / stdc++.h & gt;
using namespace std;
 
int main()
{
 
    // declaration
    unordered_multimap& lt;
    int, int& gt;
    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& lt;
    <
    "
    键和元素:\n& quot;
    ;
    for (auto it = sample.begin(); it != sample.end(); it++)
        cout& lt;
    <
    "
    "
    <
    <
    it - >
    first& lt;
    <
    "
    \t& quot;
    <
    <
    it - >
    second& lt;
    <
    endl;
 
    return 0;
}  
输出:
键和元素: 
   2          3
   2          3
   1          2
   3          4
   3          4
程序2:
// C++ program to illustrate the
// unordered_multimap::end(bucket) function
 
#include& lt; bits / stdc++.h & gt;
using namespace std;
 
int main()
{
 
    // declaration
    unordered_multimap& lt;
    int, int& gt;
    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& lt;
    <
    "
    第一个bucket的键和元素:\n& quot;
    ;
    for (auto it = sample.begin(1); it != sample.end(1);
         it++)
        cout& lt;
    <
    "
    "
    <
    <
    it - >
    first& lt;
    <
    "
    \t& quot;
    <
    <
    it - >
    second& lt;
    <
    endl;
 
    return 0;
}  
输出:
第一个bucket的键和元素: 
   1          2
让我们以下表格的形式看看它们之间的不同:
| ID | unordered_multimap::begin | unordered_multimap::end | 
|---|---|---|
| 1. | 用于返回指向unordered_multimap容器或其桶中第一个元素的迭代器 | 用于返回指向unordered_multimap容器或其桶中末尾元素之后的迭代器 | 
| 2. | 语法为:iterator begin() | 
语法为: iterator end() | 
| 3. | 接受一个参数,即桶的编号。 | 接受一个参数,即桶的编号。 | 
| 4. | 复杂度是常量级的。 | 复杂度是常量级的。 | 
| 5. | 迭代器的有效性不会改变。 | 迭代器的有效性不会改变。 | 
极客教程