set::emplace_hint
在适当的位置插入构造的元素(不执行复制或移动操作),附带位置提示。
template<class... Args>
iterator emplace_hint(
const_iterator where,
Args&&... args);
参数
参数 |
说明 |
args |
参数指向构造将要插入映射的元素,除非映射已包含该元素或,更普遍地,除非它已经包含键相同地排序的元素。 |
where |
开始搜索正确的插入点的位置。(假如点突然超出where,插入将发生在常量级时间内而不出逻辑时间内)。 |
返回值
指向新插入元素的迭代器。
如果插入失败,因元素已经存在,则返回存在存在带关键字值的元素的迭代器。
备注
没有迭代器或通过此函数的引用是无效的。
在建立期间,如果抛出了异常,容器的状态将不会被修改。
示例
// 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: " << endl;
for (const auto& p : s) {
cout << "(" << p << ") ";
}
cout << endl;
}
int main()
{
set<string> s1;
// Emplace some test data
s1.emplace("Anna");
s1.emplace("Bob");
s1.emplace("Carmine");
cout << "set starting data: ";
print(s1);
cout << endl;
// Emplace with hint
// s1.end() should be the "next" element after this emplacement
s1.emplace_hint(s1.end(), "Doug");
cout << "set modified, now contains ";
print(s1);
cout << endl;
}
Output
set starting data: 3 elements:
(Anna) (Bob) (Carmine)
set modified, now contains 4 elements:
(Anna) (Bob) (Carmine) (Doug)
要求
标头: <set>
命名空间: std