std::multiset<Key,Compare,Allocator>:: emplace
|
template
<
class
...
Args
>
iterator emplace ( Args && ... args ) ; |
(C++11以降)
(constexpr C++26以降) |
|
与えられた args を使用して、その場で構築された新しい要素をコンテナに挿入します。
新しい要素のコンストラクタは、
emplace
に提供された引数と全く同じ引数で呼び出され、
std::
forward
<
Args
>
(
args
)
...
によって転送されます。
value_type
が
EmplaceConstructible
ではなく、
args
から
multiset
への構築が不可能な場合、動作は未定義です。
イテレータおよび参照は無効化されません。
目次 |
パラメータ
| args | - | 要素のコンストラクタに転送する引数 |
戻り値
挿入された要素へのイテレータ。
例外
何らかの理由で例外がスローされた場合、この関数は何も効果を持ちません( strong exception safety guarantee )。
計算量
コンテナのサイズに対して対数的。
注記
emplace
の注意深い使用により、新しい要素を構築する際に不必要なコピー操作やムーブ操作を回避することができます。
例
#include <chrono> #include <cstddef> #include <functional> #include <iomanip> #include <iostream> #include <string> #include <set> class Dew { private: int a, b, c; public: Dew(int _a, int _b, int _c) : a(_a), b(_b), c(_c) {} bool operator<(const Dew& other) const { return (a < other.a) || (a == other.a && b < other.b) || (a == other.a && b == other.b && c < other.c); } }; constexpr int nof_operations{101}; std::size_t set_emplace() { std::multiset<Dew> set; for (int i = 0; i < nof_operations; ++i) for (int j = 0; j < nof_operations; ++j) for (int k = 0; k < nof_operations; ++k) set.emplace(i, j, k); return set.size(); } std::size_t set_insert() { std::multiset<Dew> set; for (int i = 0; i < nof_operations; ++i) for (int j = 0; j < nof_operations; ++j) for (int k = 0; k < nof_operations; ++k) set.insert(Dew(i, j, k)); return set.size(); } void time_it(std::function<int()> set_test, std::string what = "") { const auto start = std::chrono::system_clock::now(); const auto the_size = set_test(); const auto stop = std::chrono::system_clock::now(); const std::chrono::duration<double, std::milli> time = stop - start; if (!what.empty() && the_size) std::cout << std::fixed << std::setprecision(2) << time << " for " << what << '\n'; } int main() { time_it(set_insert, "cache warming..."); time_it(set_insert, "insert"); time_it(set_insert, "insert"); time_it(set_emplace, "emplace"); time_it(set_emplace, "emplace"); }
出力例:
499.61ms for cache warming... 447.89ms for insert 436.77ms for insert 430.62ms for emplace 428.61ms for emplace
関連項目
|
(C++11)
|
ヒントを使用して要素をその場で構築する
(公開メンバ関数) |
|
要素を挿入する
またはノード
(C++17以降)
(公開メンバ関数) |