當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ multimap emplace()用法及代碼示例

C++ multimap emplace() 函數用於通過向容器中插入新元素來擴展多映射容器。元素是直接構建的(既不複製也不移動)。

通過提供傳遞給此函數的參數 args 來調用元素的構造函數。

用法

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

參數

args: 為構造要插入到多映射中的元素而轉發的參數。

返回值

C++ emplace() 函數指示插入是否發生並返回指向新插入元素的迭代器。

複雜度

容器大小的對數。

迭代器有效性

沒有變化。

數據競爭

容器被修改。

盡管並發訪問現有元素是安全的,但在容器中迭代範圍並不安全。

異常安全

如果拋出異常,則容器中沒有變化。

例子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

在上麵的示例中,它隻是將元素插入具有給定鍵值對的多重映射 m 中。

例子2

讓我們看一個簡單的例子來插入元素並檢查 multimap 中是否允許重複鍵:

#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)

在上麵的示例中,元素被插入到多重映射中,當您嘗試添加相同的鍵 Nikita 時,它將允許您插入重複項。

例子3

讓我們看一個簡單的例子,通過將構造函數參數分別傳遞給鍵和值來將元素插入到多重映射中:

#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

在上麵的例子中,元素通過將構造函數參數分別傳遞給鍵和值來插入到多重映射中。

示例 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

在上麵的例子中,它隻是根據用戶的選擇插入元素。





相關用法


注:本文由純淨天空篩選整理自 C++ multimap emplace() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。