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

📅  最后修改于: 2020-10-18 03:52:15             🧑  作者: Mango

C++ Map.emplace()函数

C++ map emplace()函数用于通过将新元素插入容器来扩展地图容器。元素是直接构建的(既不复制也不移动)。

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

句法

template 
    pair emplace (Args&&... args);    //since C++ 11

参数

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

返回值

它返回一个布尔对,将指示是否发生了插入,并返回一个指向新插入元素的迭代器。

例子1

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

#include 
#include 

using namespace std;

int main(void) {
   
   map m;

   m.emplace('a', 1);
   m.emplace('b', 2);
   m.emplace('c', 3);
   m.emplace('d', 4);
   m.emplace('e', 5);

   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 = 1
b = 2
c = 3
d = 4
e = 5

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

例子2

让我们看一个简单的示例,插入元素并检查重复键。

#include   
#include   
#include   
  
using namespace std;  
  
template  void print(const M& m) {  
    cout << m.size() << " elements: ";  
  
    for (const auto& p : m) {  
        cout << "(" << p.first << ", " << p.second << ") ";  
    }  
  
    cout << endl;  
}  
  
int main()  
{  
    map m1;  
  
    auto ret = m1.emplace(10, "ten");
    ret = m1.emplace(20, "twenty");
    ret= m1.emplace(30,"thirty");
  
    if (!ret.second){  
        auto pr = *ret.first;  
        cout << "Emplace failed, element with key 10 already exists."  
            << endl << "  The existing element is (" << pr.first << ", " << pr.second << ")"  
            << endl;  
        cout << "map not modified" << endl;  
    }  
    else{  
        cout << "map modified, now contains \n";  
        print(m1);  
    }  
    cout << endl;  
  
    ret = m1.emplace(10, "one zero");  
  
    if (!ret.second){  
        auto pr = *ret.first;  
        cout << "Emplace failed, element with key 10 already exists."  
            << endl << "  The existing element is (" << pr.first << ", " << pr.second << ")"  
            << endl;  
    }  
    else{  
        cout << "map modified, now contains ";  
        print(m1);  
    }  
    cout << endl;  
}  

输出:

  map modified, now contains 
3 elements: (10, ten) (20, twenty) (30, thirty) 

Emplace failed, element with key 10 already exists.
  The existing element is (10, ten)

在上面的示例中,元素插入了地图,当您尝试使用相同的键10时,它将显示一条错误消息,提示键10已经存在。

例子3

让我们看一个简单的示例,分别通过将构造函数参数传递给键和值,将元素插入地图。

#include 
#include 
#include 
 
using namespace std;
 
#include 
int main()
{
    map m;
 
    // uses pair's move constructor
    m.emplace(make_pair(string("a"), string("a")));
 
    // uses pair's converting move constructor
    m.emplace(make_pair("b", "abcd"));
 
    // uses pair's template constructor
    m.emplace("d", "ddd");
 
    // uses pair's piecewise constructor
    m.emplace(piecewise_construct,
              forward_as_tuple("c"),
              forward_as_tuple(10, 'c'));
 
    for (const auto &p : m) {
        cout << p.first << " => " << p.second << '\n';
    }
}

输出:

a => a
b => abcd
c => cccccccccc
d => ddd

在上面的示例中,通过分别向键和值传递构造函数参数将元素插入到映射中。

例子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[name] = age; // Put them in map
       fmly.emplace(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

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