public member function
<unordered_set>
template <class... Args> iterator emplace ( Args&&... args );
Construct and insert element
Inserts a new element in the unordered_multiset. This new element is constructed in place using args as the arguments for the element's constructor.
This effectively increases the container size by one.
A similar member function exists, insert, which either copies or moves existing objects into the container.
引數
- args
- 傳遞給要插入的新元素的建構函式的引數。
返回值
指向新插入元素的迭代器。
成員型別iterator是一個 forward iterator 型別。
unordered_multiset 中的所有迭代器都對元素具有 const 訪問許可權:可以插入或移除元素,但在容器中不能修改它們。
新元素的儲存使用allocator_traits<allocator_type>::construct()分配,這在失敗時可能會丟擲異常(對於預設的 allocator,bad_alloc如果分配請求不成功,則丟擲)。
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// unordered_multiset::emplace
#include <iostream>
#include <string>
#include <unordered_set>
int main ()
{
std::unordered_multiset<std::string> myums;
myums.emplace ("milk");
myums.emplace ("tea");
myums.emplace ("coffee");
myums.emplace ("milk");
std::cout << "myums contains:";
for (const std::string& x: myums) std::cout << " " << x;
std::cout << std::endl;
return 0;
}
|
可能的輸出
myset contains: tea coffee milk milk
|
複雜度
平均情況:常量。
最壞情況:容器大小的線性。
可能會強制執行 rehash(未包含)。
迭代器有效性
在大多數情況下,插入後容器中的所有迭代器都保持有效。唯一的例外是當容器的增長強制重新雜湊時。在這種情況下,容器中的所有迭代器都將失效。
A rehash is forced if the new container size after the insertion operation would increase above its capacity threshold (calculated as the container's bucket_count multiplied by its max_load_factor).
在所有情況下,容器中元素的引用在 rehash 之後仍然有效。