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


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


集是一種關聯容器,其中每個元素都必須是唯一的,因為元素的值可以標識它。元素的值一旦添加到集合中就無法修改,盡管可以刪除並添加該元素的修改後的值。

set::emplace()

僅當要插入的元素是唯一的並且在集合中尚不存在時,才可以使用此函數將新元素插入到集合容器中。

用法:


setname.emplace(value)
參數:
The element to be inserted into the set
is passed as the parameter.
Result:
The parameter is added to the set if 
the set does not contain that element already.

例子:

Input :myset{1, 2, 3, 4, 5};
         myset.emplace(6);
Output:myset = 1, 2, 3, 4, 5, 6

Input :myset{1, 2, 3, 4, 5};
         myset.emplace(4);
Output:myset = 1, 2, 3, 4, 5

錯誤和異常
1.它具有強大的異常保證,因此,如果引發異常,則不會進行任何更改
2.參數應與容器的類型相同,否則將引發錯誤

// INTEGER SET EXAMPLE 
// CPP program to illustrate 
// Implementation of emplace() function 
#include <iostream> 
#include <set> 
using namespace std; 
  
int main() 
{ 
    set<int> myset{}; 
    myset.emplace(2); 
    myset.emplace(6); 
    myset.emplace(8); 
    myset.emplace(9); 
    myset.emplace(0); 
    // set becomes 0, 2, 6, 8, 9 
  
    // adding unique element 
  
    myset.emplace(5); 
    // set becomes 0, 2, 5, 6, 8, 9 
  
    // adding element which already 
    // exists there will be no 
    // change in the set 
  
    myset.emplace(2); 
    // set remains 0, 2, 5, 6, 8, 9 
  
    // printing the set 
    for (auto it = myset.begin(); 
         it != myset.end(); ++it) 
        cout << ' ' << *it; 
    return 0; 
}

輸出:

0 2 5 6 8 9
// STRING SET EXAMPLE 
// CPP program to illustrate 
// Implementation of emplace() function 
#include <iostream> 
#include <set> 
#include <string> 
using namespace std; 
  
int main() 
{ 
    set<string> myset{}; 
    myset.emplace("This"); 
    myset.emplace("is"); 
    myset.emplace("a"); 
    myset.emplace("computer science"); 
    myset.emplace("portal"); 
    // set becomes This, a, computer 
    // science, is, portal 
  
    // adding unique element 
    myset.emplace("GeeksForGeeks"); 
    // set becomes GeeksForGeeks, This, is, 
    // a, computer science, portal 
  
    // adding element which already exists 
    // there will be no change in the set 
    myset.emplace("is"); 
  
    // set remains GeeksForGeeks, This, is, 
    // a, computer science, portal 
  
    // printing the set 
    for (auto it = myset.begin(); 
         it != myset.end(); ++it) 
        cout << ' ' << *it; 
    return 0; 
}

輸出:

GeeksForGeeks This a computer science is portal

時間複雜度:O(登錄)

應用
使用emplace()函數輸入具有以下數字和順序的空多重集,並找到元素的總和。

Input: 7, 9, 4, 6, 2, 5, 3
Output:36
// CPP program to illustrate 
// Application of emplace() function 
#include <iostream> 
#include <set> 
using namespace std; 
  
int main() 
{ 
    // sum variable declaration 
    int sum = 0; 
  
    // set declaration 
    set<int> myset{}; 
    myset.emplace(7); 
    myset.emplace(9); 
    myset.emplace(4); 
    myset.emplace(6); 
    myset.emplace(2); 
    myset.emplace(5); 
    myset.emplace(3); 
  
    // iterator declaration 
    set<int>::iterator it; 
  
    // finding sum of elements 
    while (!myset.empty()) { 
        it = myset.begin(); 
        sum = sum + *it; 
        myset.erase(it); 
    } 
  
    // printing the sum 
    cout << sum; 
    return 0; 
}

輸出:

36

emplace()與插入
當使用插入時,我們創建一個對象,然後將其插入到多集中。使用emplace(),該對象就地構建。

// C++ code to demonstrate difference between 
// emplace and insert 
#include<bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // declaring set 
    set<pair<char, int>> ms; 
      
    // using emplace() to insert pair in-place 
    ms.emplace('a', 24); 
      
    // Below line would not compile 
    // ms.insert('b', 25);     
      
    // using emplace() to insert pair in-place 
    ms.insert(make_pair('b', 25));     
      
    // printing the set 
    for (auto it = ms.begin(); it != ms.end(); ++it) 
        cout << " " << (*it).first << " " 
             << (*it).second << endl; 
  
    return 0; 
}

輸出:

 a 24
 b 25


相關用法


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