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

C++ multimap emplace()

C++ multimap emplace()函数

C++ multimap emplace()函数用于通过在容器中插入新元素来扩展multimap容器。元素是直接构建的(既不能复制也不能移动)。
通过给传递给该函数的参数args来调用元素的构造函数。

语法

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

参数

参数: 为构造要插入到 multimap 中的元素而转发的参数。

返回值

C++ emplace()函数指示是否发生插入,并返回指向新插入元素的迭代器。

Complexity

容器大小的对数。

迭代器有效性

无变化。

数据竞争

容器已修改。
尽管同时访问出口元素是安全的,但容器中的迭代范围并不安全。

异常安全性

如果引发异常,则容器中没有任何更改。

示例1

让我们看一下插入元素的简单示例。进入 multimap :
#include <iostream>
#include <map>
using namespace std;
int main(void) {
   
   multimap<char, int> m;
   m.emplace('a', 1);
   m.emplace('b', 2);
   m.emplace('c', 3);
   m.emplace('b', 4);
   m.emplace('c', 5);
   cout << "Multimap contains following elements" << endl;
   for (auto it = m.begin(); it != m.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}
输出:
Multimap contains following elements
a = 1
b = 2
b = 4
c = 3
c = 5
在上面的示例中,它只是将具有给定键值对的元素插入到multimap中。

示例2

让我们看一个简单的插入元素并检查 set 地图中是否允许使用重复键的示例:
#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()  
{  
    multimap<string, string> m1;  
  
    m1.emplace("Nikita", "Accounting");  
    m1.emplace("Amita", "Accounting");  
    m1.emplace("Deep", "Engineering");  
  
    cout << "multimap modified, now contains ";  
    print(m1);  
    cout << endl;  
  
    m1.emplace("Nikita", "Engineering");  
  
    cout << "multimap modified, now contains ";  
    print(m1);  
    cout << endl;  
}
输出:
multimap modified, now contains 3 elements: 
(Amita,Accounting) (Deep,Engineering) (Nikita,Accounting) 
multimap modified, now contains 4 elements: 
(Amita,Accounting) (Deep,Engineering) (Nikita,Accounting) (Nikita,Engineering)
在上面的示例中,将元素插入了 multimap ,当您尝试添加相同的键Nikita时,它将允许您插入重复项。

示例3

让我们看一个简单的示例,通过将构造函数参数分别传递给键和值来将元素插入 multimap :
#include <iostream>
#include <utility>
#include <string>
#include <map>
 
using namespace std;
int main()
{
    multimap<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("a", "aaa");
 
    // 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
a => aaa
b => abcd
c => cccccccccc
在上面的示例中,分别通过将构造函数参数传递给键和值来将元素插入 multimap 。

示例4

参见插入元素的简单示例:
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
  typedef multimap<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(name,age); // Put them in multimap
       
   }
   
      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