C++ map emplace()函数

C++ map emplace()函数

C++ map emplace() 函数用于通过将新元素插入到容器中来扩展map容器。元素是直接构建的(不会被复制或移动)。

给定用作参数传递给此函数的args,通过调用元素的构造函数来构造元素。 只有在键不存在的情况下才会进行插入。

语法

template <class...Args>
    pair<iterator, bool> emplace (Args&&... args);    //since C++ 11

参数

args :用于构造要插入地图中的元素的参数。

返回值

It 返回一个bool对,表示插入是否发生,并返回一个指向新插入元素的迭代器。

示例1

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

#include <iostream>
#include <map>

using namespace std;

int main(void) {

   map<char, int> 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 <map>  
#include <string>  
#include <iostream>  

using namespace std;  

template <typename M> void print(const M& m) {  
    cout << m.size() << " elements: ";  

    for (const auto& p : m) {  
        cout << "(" << p.first << ", " << p.second << ") ";  
    }  

    cout << endl;  
}  

int main()  
{  
    map<int, string> 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 <iostream>
#include <utility>
#include <string>

using namespace std;

#include <map>
int main()
{
    map<string, string> 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 <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[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

在以上示例中,它只是按照用户的选择插入元素。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程