在C++ STL中的unOrdEdMultIMAP EngestEffice()函数

这个 无序的多重映射::emplace() 是C++ STL中的一个内置函数,它在无序的MultIMAP容器中插入新的{键,元素}。根据容器的标准,插入会自动在该位置完成。它将容器的大小增加了一倍。

null

语法:

unordered_multimap_name.emplace(key, element)

参数: 该函数只接受一个强制参数 钥匙 要素 它将被插入容器中。

返回值: 它返回一个迭代器,该迭代器指向新插入的元素。

以下程序说明了上述功能:

项目1:

// C++ program to illustrate
// unordered_multimap::emplace()
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
// declaration
unordered_multimap< int , int > sample;
// inserts key and elements
sample.emplace(1, 2);
sample.emplace(1, 2);
sample.emplace(1, 3);
sample.emplace(4, 9);
sample.emplace(60, 89);
cout << "Key and Elements: " ;
for ( auto it = sample.begin(); it != sample.end(); it++)
cout << "{" << it->first << ":" << it->second << "} " ;
return 0;
}


输出:

Key and Elements: 
 {60:89}
 {4:9}
 {1:3}
 {1:2}
 {1:2}

项目2:

// unordered_multimap::emplace
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
// declaration
unordered_multimap<string, string> sample;
// inserts key and elements
sample.emplace( "gopal" , "dave" );
sample.emplace( "gopal" , "dave" );
sample.emplace( "Geeks" , "C++" );
sample.emplace( "multimap" , "functions" );
sample.emplace( "multimap" , "functions" );
cout << "Key and Elements: " ;
for ( auto it = sample.begin(); it != sample.end(); it++)
cout << "{" << it->first << ":" << it->second << "} " ;
return 0;
}


输出:

Key and Elements: 
 {multimap:functions}
 {multimap:functions}
 {Geeks:C++}
 {gopal:dave}
 {gopal:dave}

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