📜  C++ STL中的unordered_map end()函数

📅  最后修改于: 2021-05-30 05:15:52             🧑  作者: Mango

unordered_map :: end()是C++ STL中的内置函数,它返回一个迭代器,该迭代器指向unordered_map容器中容器中最后一个元素之后的位置。在unordered_map对象中,不能保证将哪个特定元素视为其第一个元素。但是容器中的所有元素都被覆盖了,因为范围从其开始到结束直到无效。

句法:

iterator unordered_map_name.end(n)

参数:此函数接受一个参数n ,它是指定存储桶编号的可选参数。如果设置了该值,则迭代器将获取指向存储桶的past-the-end元素的点,否则,它将指向容器的past-the-end元素。

返回值:该函数将迭代器返回到容器末端之后的元素。

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

程序1:

// CPP program to demonstrate the
// unordered_map::end() function
// returning all the elements of the multimap
#include 
#include 
#include 
using namespace std;
  
int main()
{
    unordered_map marks;
  
    // Declaring the elements of the multimap
    marks = { { "Rohit", 64 }, { "Aman", 37 }, { "Ayush", 96 } };
  
    // Printing all the elements of the multimap
    cout << "marks bucket contains : " << endl;
    for (int i = 0; i < marks.bucket_count(); ++i) {
  
        cout << "bucket #" << i << " contains:";
  
        for (auto iter = marks.begin(i); iter != marks.end(i); ++iter) {
            cout << "(" << iter->first << ", " << iter->second << "), ";
        }
  
        cout << endl;
    }
    return 0;
}
输出:
marks bucket contains : 
bucket #0 contains:
bucket #1 contains:
bucket #2 contains:
bucket #3 contains:(Aman, 37), (Rohit, 64), 
bucket #4 contains:(Ayush, 96),

程序2

// CPP program to demonstrate the
// unordered_map::end() function
// returning the elements along
// with their bucket number
#include 
#include 
#include 
  
using namespace std;
  
int main()
{
    unordered_map marks;
  
    // Declaring the elements of the multimap
    marks = { { "Rohit", 64 }, { "Aman", 37 }, { "Ayush", 96 } };
  
    // Printing all the elements of the multimap
    for (auto iter = marks.begin(); iter != marks.end(); ++iter) {
  
        cout << "Marks of " << iter->first << " is "
             << iter->second << " and his bucket number is "
             << marks.bucket(iter->first) << endl;
    }
  
    return 0;
}
输出:
Marks of Ayush is 96 and his bucket number is 4
Marks of Aman is 37 and his bucket number is 3
Marks of Rohit is 64 and his bucket number is 3
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”