在适当位置插入构建元素 (不执行复制或移动操作) 。
template<class... Args>
pair<iterator, bool> emplace(
Args&&... args
);
参数
参数 |
说明 |
args |
参数指向构造要插入集合的元素,除非它已经包含值相同地排序的元素。 |
返回值
一个pair 逻辑组件如果已插入那就是真的, 如果映射已经包含值相同地排序的元素就是假的. 返回值的迭代器元素对返回插入新元素的地址 (如果 bool 元素为 true) 或已找到其中的元素 (如果 bool 元素是假)。
备注
没有迭代器或通过此函数的引用是无效的。
在建立期间,如果抛出了异常,容器的状态将不会被修改。
示例
// set_emplace.cpp
// compile with: /EHsc
#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;
}
Output
set modified, now contains 1 elements: (ten)
Emplace failed, element with value "ten" already exists.
The existing element is (ten)
要求
标头: <set>
命名空间: std