当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。