การทำงานของ emplace หลีกเลี่ยงการคัดลอกวัตถุที่ไม่จำเป็น และทำการแทรกได้อย่างมีประสิทธิภาพมากกว่าการใส่ การดำเนินการแทรกจะใช้การอ้างอิงถึงวัตถุ
อัลกอริทึม
Begin Declare set. Use emplace() to insert pair. Use insert() to insert pair by using emplace(). Print the set. End
โค้ดตัวอย่าง
#include<bits/stdc++.h>
using namespace std;
int main() {
set<pair<int, char>> s;
s.emplace(7, 'a');
s.insert(make_pair(6, 'b'));
for (auto it = s.begin(); it != s.end(); ++it)
cout << " " << (*it).first << " " << (*it).second << endl;
return 0;
} ผลลัพธ์
7 a 6 b