名前空間
変種
操作

std::set<Key,Compare,Allocator>::emplace_hint

提供: cppreference.com
< cpp‎ | container‎ | set
 
 
 
 
template <class... Args>
iterator emplace_hint( const_iterator hint, Args&&... args );
(C++11以上)

新しい要素をコンテナ内の hint の直前の位置に可能な限り近い位置に挿入します。 要素はその場で構築されます。 つまり、コピーもムーブも行われません。

要素のコンストラクタは、この関数に与えられたものとまったく同じ引数が std::forward<Args>(args)... で転送されて、呼ばれます。

どのイテレータも参照も無効化されません。

目次

[編集] 引数

hint - 前に新しい要素が挿入される位置のイテレータ
args - 要素のコンストラクタに転送される引数

[編集] 戻り値

新しく挿入された要素を指すイテレータを返します。

要素がすでに存在するための挿入が失敗した場合、等しいキーを持つ既存の要素を指すイテレータを返します。

[編集] 例外

何らかの操作によって例外が投げられる場合、この関数は効果を持ちません (強い例外保証)。

[編集] 計算量

一般的にはコンテナのサイズの対数。 新しい要素が hint の直前に挿入される場合は償却定数時間。

[編集]

#include <set>
#include <chrono>
#include <iostream>
#include <iomanip>
#include <functional>
 
const int nof_operations = 100500;
 
int set_emplace() {
  std::set<int> set;
  for(int i = 0; i < nof_operations; ++i) {
    set.emplace(i);
  }
  return set.size();
}
 
int set_emplace_hint() {
  std::set<int> set;
  auto it = set.begin();
  for(int i = 0; i < nof_operations; ++i) {
    set.emplace_hint(it, i);
    it = set.end();
  }
  return set.size();
}
 
int set_emplace_hint_wrong() {
  std::set<int> set;
  auto it = set.begin();
  for(int i = nof_operations; i > 0; --i) {
    set.emplace_hint(it, i);
    it = set.end();
  }
  return set.size();
}
 
int set_emplace_hint_corrected() {
  std::set<int> set;
  auto it = set.begin();
  for(int i = nof_operations; i > 0; --i) {
    set.emplace_hint(it, i);
    it = set.begin();
  }
  return set.size();
}
 
int set_emplace_hint_closest() {
  std::set<int> set;
  auto it = set.begin();
  for(int i = 0; i < nof_operations; ++i) {
    it = set.emplace_hint(it, i);
  }
  return set.size();
}
 
void timeit(std::function<int()> set_test, std::string what = "") {
  auto start = std::chrono::system_clock::now();
  int setsize = set_test();
  auto stop = std::chrono::system_clock::now();
  std::chrono::duration<double, std::milli> time = stop - start;
  if (what.size() > 0 && setsize > 0) {
    std::cout << std::fixed << std::setprecision(2)
              << time.count() << "  ms for " << what << '\n';
  }
}
 
int main() {
   timeit(set_emplace); // stack warmup
   timeit(set_emplace, "plain emplace");
   timeit(set_emplace_hint, "emplace with correct hint");
   timeit(set_emplace_hint_wrong, "emplace with wrong hint");
   timeit(set_emplace_hint_corrected, "corrected emplace");
   timeit(set_emplace_hint_closest, "emplace using returned iterator");
}

出力例:

18.96  ms for plain emplace
7.95  ms for emplace with correct hint
19.39  ms for emplace with wrong hint
8.39  ms for corrected emplace
7.90  ms for emplace using returned iterator

[編集] 関連項目

(C++11)
要素をその場で構築します
(パブリックメンバ関数) [edit]
要素またはノード (C++17以上)を挿入します
(パブリックメンバ関数) [edit]