template <class... Args>
pair<iterator,bool> emplace (Args&&... args);
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main()
{
//创建并初始化 set 容器
std::set<string>myset;
//向 myset 容器中添加元素
pair<set<string, string>::iterator, bool> ret = myset.emplace("http://c.biancheng.net/stl/");
cout << "myset size = " << myset.size() << endl;
cout << "ret.iter = <" << *(ret.first) << ", " << ret.second << ">" << endl;
return 0;
}
程序执行结果为:
myset size = 1
ret.iter = <http://c.biancheng.net/stl/, 1>
template <class... Args>
iterator emplace_hint (const_iterator position, Args&&... args);
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main()
{
//创建并初始化 set 容器
std::set<string>myset;
//在 set 容器的指定位置添加键值对
set<string>::iterator iter = myset.emplace_hint(myset.begin(), "http://c.biancheng.net/stl/");
cout << "myset size = " << myset.size() << endl;
cout << *iter << endl;
return 0;
}
程序执行结果为:
myset size = 1
http://c.biancheng.net/stl/
以上内容讲解了 emplace() 和 emplace_hint() 的用法,至于比 insert() 执行效率高的原因,可参照 map 容器 emplace() 和 emplace_hint() 比 insert() 效率高的原因,它们是完全一样的,这里不再赘述。
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有