名前空間
変種
操作

std::multiset<Key,Compare,Allocator>::emplace

From cppreference.com
 
 
 
 
template< class... Args >
iterator emplace( Args&&... args );
(C++11以降)

与えられた args で構築された新しい要素をコンテナにインプレースで挿入します。

新しい要素のコンストラクタは、emplace に供給された引数とまったく同じ引数で呼び出されます。これらの引数は、std::forward<Args>(args)... を介して転送されます。

emplace を注意深く使用することで、不要なコピーまたはムーブ操作を回避しながら新しい要素を構築できます。

イテレータや参照は無効化されない。

目次

[編集] パラメータ

args - 要素のコンストラクタに転送する引数

[編集] 戻り値

挿入された要素へのイテレータ。

[編集] 例外

何らかの理由で例外がスローされた場合、この関数は効果がありません(強力な例外安全保証)。

[編集] 計算量

コンテナのサイズに対して対数時間。

[編集]

#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++17以降)を挿入する
(公開メンバ関数) [編集]
English 日本語 中文(简体) 中文(繁體)