std::multiset<Key,Compare,Allocator>:: emplace
|
template
<
class
...
Args
>
iterator emplace ( Args && ... args ) ; |
(seit C++11)
(constexpr seit C++26) |
|
Fügt ein neues Element in den Container ein, das direkt mit den gegebenen args erstellt wird.
Der Konstruktor des neuen Elements wird mit exakt denselben Argumenten aufgerufen, die an
emplace
übergeben wurden, weitergeleitet via
std::
forward
<
Args
>
(
args
)
...
.
Wenn
value_type
nicht
EmplaceConstructible
in
multiset
aus
args
ist, ist das Verhalten undefiniert.
Keine Iteratoren oder Referenzen werden ungültig.
Inhaltsverzeichnis |
Parameter
| args | - | Argumente, die an den Konstruktor des Elements weitergeleitet werden |
Rückgabewert
Ein Iterator auf das eingefügte Element.
Ausnahmen
Wenn aus irgendeinem Grund eine Exception ausgelöst wird, hat diese Funktion keine Wirkung ( strong exception safety guarantee ).
Komplexität
Logarithmisch in der Größe des Containers.
Hinweise
Sorgfältige Verwendung von
emplace
ermöglicht die Konstruktion des neuen Elements, während unnötige Kopier- oder Verschiebeoperationen vermieden werden.
Beispiel
#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 << " für " << what << '\n'; } int main() { time_it(set_insert, "Cache-Aufwärmung..."); time_it(set_insert, "insert"); time_it(set_insert, "insert"); time_it(set_emplace, "emplace"); time_it(set_emplace, "emplace"); }
Mögliche Ausgabe:
499.61ms für cache warming... 447.89ms für insert 436.77ms für insert 430.62ms für emplace 428.61ms für emplace
Siehe auch
|
(C++11)
|
Konstruiert Elemente direkt unter Verwendung eines Hinweises
(öffentliche Elementfunktion) |
|
Fügt Elemente ein
oder Knoten
(seit C++17)
(öffentliche Elementfunktion) |