C++ map emplace_hint() 函数

C++ map emplace_hint() 函数

C++ map emplace_hint() 函数用于通过使用提示作为元素的位置,在容器中插入新元素来扩展map容器。元素是直接建立的(既不复制也不移动)。

调用元素的构造函数时,将参数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

在上面的示例中,它只是在地图的开头插入用户选择的元素。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程