C++ Set 中的 emplace函数

首页 / C++入门教程 / C++ Set 中的 emplace函数

C++ set emplace()函数用于通过将新元素插入集合来扩展set集合。元素是直接构建的(既不复制也不移动)。

通过给传递给该函数的参数args调用元素的构造函数。

无涯教程网

emplace - 语法

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

emplace - 参数

args : 构造要插入到集合中的元素的参数。

emplace - 返回值

emplace()函数返回布尔对,该布尔对将指示是否发生插入,并返回指向新插入元素的迭代器。

emplace - 例子1

让我们看一下将元素插入到集合中的简单示例:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   
   set<char> m;

   m.emplace('a');
   m.emplace('b');
   m.emplace('c');
   m.emplace('d');
   m.emplace('e');

   cout << "Set contains following elements" << endl;

   for (auto it = m.begin(); it != m.end(); ++it)
      cout << *it<< ", ";

   return 0;
}

输出:

Set contains following elements
a, b, c, d, e,

在上面的示例中,它只是将具有给定键值对的元素插入到集合m中。

emplace - 例子2

让我们看一个简单的例子,插入元素并检查重复键:

#include <set>  
#include <string>  
#include <iostream>  
  
using namespace std;  
  
template <typename S> void print(const S& s) {  
    cout << s.size() << " elements: ";  
  
    for (const auto& p : s) {  
        cout << "(" << p << ") ";  
    }  
  
    cout << endl;  
}  
  
int main()  
{  
    set<string> s1;  
  
    auto ret = s1.emplace("ten");  
  
    if (!ret.second){  
        cout << "Emplace failed, element with value \"ten\" already exists."  
            << endl << "  The existing element is (" << *ret.first << ")"  
            << endl;  
        cout << "set not modified" << endl;  
    }  
    else{  
        cout << "set modified, now contains ";  
        print(s1);  
    }  
    cout << endl;  
  
    ret = s1.emplace("ten");  
  
    if (!ret.second){  
        cout << "Emplace failed, element with value \"ten\" already exists."  
            << endl << "  The existing element is (" << *ret.first << ")"  
            << endl;  
    }  
    else{  
        cout << "set modified, now contains ";  
        print(s1);  
    }  
    cout << endl;  
}  

输出:

  set modified, now contains 1 elements: (ten) 

Emplace failed, element with value "ten" already exists.
  The existing element is (ten)

在上面的示例中,将元素插入到集合中,当您尝试使用相同的键"十"时,它将显示一条错误消息,提示键"十"已经存在。

emplace - 例子3

让我们看一个简单的示例,查找插入元素的总和:

#include <iostream>
#include <set>
using namespace std;
 
int main()
{
   //sum variable declaration
    int sum = 0;
 
   //set declaration
    set<int> myset{};
    myset.emplace(1);
    myset.emplace(7);
    myset.emplace(4);
    myset.emplace(8);
    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 of elements is: "<<sum;
    return 0;
}

输出:

Sum of elements is: 30

emplace - 例子4

让我们看一个插入元素的简单示例:

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-set-emplace-function.html

来源:LearnFk无涯教程网

#include <iostream>
#include <set>
#include <string>
using namespace std;

int main() {

  typedef set<string> city;  
   string name;
   city fmly ;
   int n;

   cout<<"Enter the number of family members :";
   cin>>n;

   cout<<"Enter the name of each member: \n";
   for(int i =0; i<n; i++)
   {
       cin>> name;     //Get key
       fmly.emplace(name);
       
   }
   
      cout<<"\nTotal member of family is:"<< fmly.size();

      cout<<"\nDetails of family members: \n";
      cout<<"\nName \n ________________________\n";
      city::iterator p;
      for(p = fmly.begin(); p!=fmly.end(); p++)
      {
          cout<<(*p)<<" \n ";
      }
    
   return 0;
}

输出:

Enter the number of family members: 3
Enter the name of each member: 
Bob
Robin
David

Total member of family is: 3
Details of family members: 

Name 
 ________________________
Bob 
David 
Robin

在上面的示例中,它只是根据用户的选择插入元素。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

大规模数据处理实战 -〔蔡元楠〕

互联网人的英语私教课 -〔陈亦峰〕

Vim 实用技巧必知必会 -〔吴咏炜〕

技术管理案例课 -〔许健〕

Go 并发编程实战课 -〔晁岳攀(鸟窝)〕

A/B测试从0到1 -〔张博伟〕

基于人因的用户体验设计课 -〔刘石〕

深入浅出可观测性 -〔翁一磊〕

现代C++20实战高手课 -〔卢誉声〕

好记忆不如烂笔头。留下您的足迹吧 :)