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

C++ Set emplace()

C++ Set emplace()

C++ Set emplace()函数用于通过将新元素插入容器来扩展集合容器。元素是直接构建的(既不能复制也不能移动)。
通过给传递给该函数的参数args来调用元素的构造函数。
仅在没有key的情况下插入

语法

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

参数

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

返回值

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中。

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

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

示例4

让我们看一个插入元素的简单示例:
#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
在上面的示例中,它只是根据用户的选择插入元素。

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4