📜  C++ STL-map.emplace_hint()函数

📅  最后修改于: 2020-10-18 11:28:59             🧑  作者: Mango

C++ map.emplace_hint()函数

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

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

句法

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

参数

args:传递来构造要插入地图的元素的参数。

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

返回值

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

例子1

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

#include 
#include 
using namespace std;
int main(void) {
   map 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   
#include   
#include   
using namespace std;  
template  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 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 
#include 
using namespace std;
int main ()
{
  map 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 
#include 
#include 
using namespace std;
int main() {
 typedef map 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> 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

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