C++ Map 中的 emplace_hint函数

首页 / C++入门教程 / C++ Map 中的 emplace_hint函数

C++ map emplace_hint()函数用于通过使用提示作为元素位置将新元素插入集合来扩展映射集合。元素是直接构建的(既不复制也不移动)。

通过给传递给该函数的参数args调用元素的构造函数。 仅当密钥不存在时才进行插入。

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-map-emplace-hint-function.html

来源:LearnFk无涯教程网

emplace_hint - 语法

template <class... Args>
    iterator emplace_hint (const_iterator position, Args&&...args);  //since C++ 11

emplace_hint - 参数

args        :转发的自变量用于构造要插入到映射中的元素。

position :提示插入新元素的位置。

emplace_hint - 返回值

它将迭代器返回到新插入的元素。如果元素已经存在,则插入将失败,并将迭代器返回到现有元素。

emplace_hint - 例子1

让我们看一个将元素插入映射的简单示例。

#include <iostream>
#include <map>
using namespace std;
int main(void) {
   map<char, int> m = {
            {'b', 20},
            {'c', 30},
            {'d', 40},
            };

   m.emplace_hint(m.end(), 'e', 50);
   m.emplace_hint(m.begin(), 'a', 10);
  cout << "Map contains following elements" << endl;
  for (auto it = m.begin(); it != m.end(); ++it)
  cout << it->first << " = " << it->second << endl;
   return 0;
}

输出:

Map contains following elements
a = 10
b = 20
c = 30
d = 40
e = 50

在上面的示例中,它只是将具有给定键值对和位置的元素插入到映射m中。

无涯教程网

emplace_hint - 例子2

让我们看一个简单的例子。

#include <map>  
#include <string>  
#include <iostream>  
using namespace std;  
template <typename M> void print(const M& m) {  
 cout << m.size() << " elements: " << endl;  
 for (const auto& p : m) {  
 cout << "(" << p.first <<  "," << p.second << ") ";  
 }  
 cout << endl;  
}  
  
int main()  
{  
    map<string, string> m1;  
  
   //Emplace some test data  
    m1.emplace("Ram", "Accounting");  
    m1.emplace("Rakesh", "Accounting");  
    m1.emplace("Sunil", "Engineering");  
    cout << "map starting data: ";  
    print(m1);  
    cout << endl;  
  //Emplace with hint  
   //m1.end() should be the "next" element after this emplacement  
    m1.emplace_hint(m1.end(), "Deep", "Engineering");  
    cout << "map modified, now contains ";  
    print(m1);  
    cout << endl;  
} 

输出:

  map starting data: 3 elements: 
(Rakesh,Accounting) (Ram,Accounting) (Sunil,Engineering) 

map modified, now contains 4 elements: 
(Deep,Engineering) (Rakesh,Accounting) (Ram,Accounting) (Sunil,Engineering)

emplace_hint - 例子3

让我们看一个简单的示例,将元素插入到具有给定位置的映射中。

#include <iostream>
#include <map>
using namespace std;
int main ()
{
  map<char,int> mymap;
  auto it = mymap.end();

  it = mymap.emplace_hint(it,'b',10);
  mymap.emplace_hint(it,'a',12);
  mymap.emplace_hint(mymap.end(),'c',14);
 cout << "mymap contains:";
  for (auto& x: mymap)
    cout << " [" << x.first << ':' << x.second << ']';
    cout << '\n';

  return 0;
}

输出:

mymap contains: [a:12] [b:10] [c:14]

emplace_hint - 例子4

让我们看一个插入元素的简单示例。

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
 typedef map<string, int> city;  
   string name;
   int age;
   city fmly ;
   int n;
  cout<<"Enter the number of fmly members :";
  cin>>n;
  cout<<"Enter the name and age of each member: \n";
   for(int i =0; i<n; i++)
   {
       cin>> name;     //Get key
       cin>> age;   //Get value

       fmly.emplace_hint(fmly.begin(),name,age);
       
   }
   
      cout<<"\nTotal memnber of fmly is:"<< fmly.size();

      cout<<"\nDetails of fmly members: \n";
      cout<<"\nName  |  Age \n ________________________\n";
      city::iterator p;
      for(p = fmly.begin(); p!=fmly.end(); p++)
      {
          cout<<(*p).first << " | " <<(*p).second <<" \n ";
      }
    
   return 0;
}

输出:

Enter the number of fmly members : 3
Enter the name and age of each member: 
Ram 42
Sita 37
Laxman 40

Total memnber of fmly is:3
Details of fmly members: 

Name    |  Age 
__________________________
Laxman | 40 
Ram      | 42 
Sita       | 37

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

React实战进阶45讲 -〔王沛〕

软件工程之美 -〔宝玉〕

从0开发一款iOS App -〔朱德权〕

零基础学Java -〔臧萌〕

OpenResty从入门到实战 -〔温铭〕

苏杰的产品创新课 -〔苏杰〕

系统性能调优必知必会 -〔陶辉〕

To B市场品牌实战课 -〔曹林〕

Serverless进阶实战课 -〔静远〕

好记忆不如烂笔头。留下您的足迹吧 :)