std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: emplace_hint
|
template
<
class
...
Args
>
iterator emplace_hint ( const_iterator hint, Args && ... args ) ; |
(C++11以降)
(constexprはC++26以降) |
|
コンテナに新しい要素を挿入します。 hint を要素の挿入位置の提案として使用します。
value_type
のコンストラクタ(すなわち、
std::
pair
<
const
Key, T
>
)は、関数に供給された引数と全く同じ引数で呼び出され、
std::
forward
<
Args
>
(
args
)
...
によって転送されます。
操作後に新しい要素数が元の
max_load_factor()
*
bucket_count()
より大きい場合、再ハッシュが行われます。
再ハッシュが発生した場合(挿入による)、すべてのイテレータは無効化されます。それ以外の場合(再ハッシュなし)、イテレータは無効化されません。
目次 |
パラメータ
| hint | - | イテレータ、新しい要素を挿入する位置の提案として使用 |
| args | - | 要素のコンストラクタに転送する引数 |
戻り値
挿入された要素、または挿入を妨げた要素へのイテレータ。
例外
何らかの理由で例外がスローされた場合、この関数は何も効果を持ちません( strong exception safety guarantee )。
計算量
償却計算では平均的に定数時間、最悪ケースではコンテナのサイズに対して線形時間。
例
#include <chrono> #include <cstddef> #include <functional> #include <iomanip> #include <iostream> #include <unordered_map> const int n_operations = 100'500'0; std::size_t map_emplace() { std::unordered_map<int, char> map; for (int i = 0; i < n_operations; ++i) map.emplace(i, 'a'); return map.size(); } std::size_t map_emplace_hint() { std::unordered_map<int, char> map; auto it = map.begin(); for (int i = 0; i < n_operations; ++i) { map.emplace_hint(it, i, 'b'); it = map.end(); } return map.size(); } std::size_t map_emplace_hint_wrong() { std::unordered_map<int, char> map; auto it = map.begin(); for (int i = n_operations; i > 0; --i) { map.emplace_hint(it, i, 'c'); it = map.end(); } return map.size(); } std::size_t map_emplace_hint_corrected() { std::unordered_map<int, char> map; auto it = map.begin(); for (int i = n_operations; i > 0; --i) { map.emplace_hint(it, i, 'd'); it = map.begin(); } return map.size(); } std::size_t map_emplace_hint_closest() { std::unordered_map<int, char> map; auto it = map.begin(); for (int i = 0; i < n_operations; ++i) it = map.emplace_hint(it, i, 'e'); return map.size(); } double time_it(std::function<std::size_t()> map_test, std::string what = "", double ratio = 0.0) { const auto start = std::chrono::system_clock::now (注:元のテキストはC++の標準ライブラリ関数名であり、HTMLタグと属性は保持されています。翻訳対象の自然言語テキストが存在しないため、出力は入力と同じになります)(); const std::size_t map_size = map_test(); const auto stop = std::chrono::system_clock::now(); std::chrono::duration<double, std::milli (注:指定された条件により、HTMLタグ・属性は保持され、C++固有用語は翻訳せず、コードタグ内ではないため「milli」はそのまま維持されています)> time = stop - start; if (what.size() && map_size) std::cout << std::setw(8) << time << " for " << what << " (比率: " << (ratio == 0.0 ? 1.0 : ratio / time.count()) << ")\n"; return time.count(); } int main() { std::cout << std::fixed << std::setprecision(2); time_it(map_emplace); // キャッシュのウォームアップ const auto x = time_it(map_emplace, "プレーン emplace"); time_it(map_emplace_hint, "正しいヒントを用いた emplace", x); time_it(map_emplace_hint_wrong, "間違ったヒントでの emplace", x); time_it(map_emplace_hint_corrected, "修正済み emplace", x); time_it(map_emplace_hint_closest, "返されたイテレータを使用したemplace", x); }
出力例:
143.48ms for plain emplace (ratio: 1.00) 164.78ms for emplace with correct hint (ratio: 0.87) 171.22ms for emplace with wrong hint (ratio: 0.84) 166.55ms for corrected emplace (ratio: 0.86) 167.41ms for emplace using returned iterator (ratio: 0.86)
関連項目
|
要素をその場で構築する
(公開メンバ関数) |
|
|
要素を挿入する
またはノード
(C++17以降)
(公開メンバ関数) |