C++ set emplace()函数用于通过将新元素插入集合来扩展set集合。元素是直接构建的(既不复制也不移动)。
通过给传递给该函数的参数args调用元素的构造函数。
template <class.... Args> pair<iterator, bool> emplace (Args&&... args); //since C++ 11
args : 构造要插入到集合中的元素的参数。
emplace()函数返回布尔对,该布尔对将指示是否发生插入,并返回指向新插入元素的迭代器。
让我们看一下将元素插入到集合中的简单示例:
#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中。
让我们看一个简单的例子,插入元素并检查重复键:
#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)
在上面的示例中,将元素插入到集合中,当您尝试使用相同的键"十"时,它将显示一条错误消息,提示键"十"已经存在。
让我们看一个简单的示例,查找插入元素的总和:
#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
让我们看一个插入元素的简单示例:
链接: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
在上面的示例中,它只是根据用户的选择插入元素。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)