C++ STL 中 unordered_multimap cend() 函数
unordered_multimap::cend() 是 C++ STL 中的内置函数,用于返回指向容器中最后一个元素之后位置的常量迭代器,或指向其中一个桶的最后一个元素之后位置的常量迭代器。
语法:
unordered_multimap_name.cend(n)
参数: 该函数接受一个参数。如果传递参数,则返回指向其中一个桶的最后一个元素之后位置的常量迭代器。如果没有传递参数,则返回指向 unordered_multimap 容器中最后一个元素之后位置的常量迭代器。
返回值: 返回常量迭代器。它不能用于修改 unordered_multimap 元素的键和元素。
下面的程序说明了上述函数:
程序 1:
// C++ program to illustrate the
// unordered_multimap::cend()
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // 声明
    unordered_multimap<int, int> sample;
  
    // 插入键和元素
    sample.insert({ 1, 2 });
    sample.insert({ 3, 4 });
    sample.insert({ 3, 4 });
    sample.insert({ 2, 3 });
    sample.insert({ 2, 3 });
  
    // 打印所有元素
    cout << "键和元素:";
    for (auto it = sample.cbegin(); it != sample.cend(); it++)
        cout << "\n   " << it->first << "\t      " 
             << it->second;
  
    return 0;
}
键和元素:
   2          3
   2          3
   1          2
   3          4
   3          4
程序 2:
// C++ program to illustrate the
// unordered_multimap::cend(bucket)
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // 声明
    unordered_multimap<int, int> sample;
  
    // 插入键和元素
    sample.insert({ 1, 2 });
    sample.insert({ 3, 4 });
    sample.insert({ 3, 4 });
    sample.insert({ 2, 3 });
    sample.insert({ 2, 3 });
  
    // 打印第一个桶的所有元素
    cout << "第一个桶的键和元素:";
    for (auto it = sample.cbegin(1); it != sample.cend(1); it++)
        cout << "\n   " << it->first << "\t      " 
             << it->second;
  
    return 0;
}
第一个桶的键和元素:
   1          2
极客教程