C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ Map emplace_hint()

C++ Map emplace_hint()函数

C++地图 emplace_hint()函数用于通过使用提示将新元素插入到容器中来扩展地图容器作为元素的位置。元素是直接构建的(既不能复制也不能移动)。
通过将参数args传递给该函数来调用元素的构造函数。仅当密钥不存在时才会插入。

语法

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

参数

args: 为构造要插入地图的元素而转发的参数。
position : 提示要插入新元素的位置。

返回值

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

示例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中。

示例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)

示例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]

示例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
在上面的示例中,它只是根据用户的选择将元素插入地图的开头。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4