C++中的无序映射

这个 无序地图::开始() 在C++ STL中是一个内置函数,它返回一个指向无序的映射容器中的第一个元素或其任何一个桶的迭代器。

null
  • 无序映射容器中第一个元素的语法:
unordered_map.begin()

参数: 此函数不接受任何参数。 返回值: 函数返回一个迭代器,指向无序映射容器中的第一个元素。 注: 在无序映射中,没有被视为第一个元素的特定元素。 下面的程序演示了上述功能。

CPP

// CPP program to demonstrate the
// unordered_map::begin() function
// when first element of the container
// is to be returned as iterator
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Declaration
unordered_map<std::string, std::string> mymap;
// Initialisation
mymap = { { "Australia" , "Canberra" },
{ "U.S." , "Washington" },
{ "France" , "Paris" } };
// Iterator pointing to the first element
// in the unordered map
auto it = mymap.begin();
// Prints the elements of the first element in map
cout << it->first << " " << it->second;
return 0;
}


输出:

France Paris

  • 无序映射桶中第一个元素的语法:
unordered_map.begin( n )

参数: 该函数接受一个强制参数 N 它指定要返回其第一个元素的迭代器的桶号。 返回值: 该函数返回一个迭代器,该迭代器指向第n个存储桶中的第一个元素。 下面的程序演示了上述功能。

CPP

// CPP program to demonstrate the
// unordered_map::begin() function
// when first element of n-th container
// is to be returned as iterator
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Declaration
unordered_map<std::string, std::string> mymap;
// Initialisation
mymap = { { "Australia" , "Canberra" },
{ "U.S." , "Washington" }, { "France" , "Paris" } };
// Iterator pointing to the first element
// in the n-th bucket
auto it = mymap.begin(0);
// Prints the elements of the n-th bucket
cout << it->first << " " << it->second;
return 0;
}


输出:

U.S. Washington

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享