(C++17) | ||||
| Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
| Associative | ||||
| Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
| Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
| Views | ||||
(C++20) | ||||
(C++23) | ||||
| Tables | ||||
| Iterator invalidation | ||||
| Member function table | ||||
| Non-member function table |
| Member functions | |||||||
| Non-member functions | |||||||
| |||||||
| Deduction guides(C++17) | |||||||
template<class...Args> std::pair<iterator,bool> emplace( Args&&...args); | (since C++11) (constexpr since C++26) | |
Inserts a new element into the container constructed in-place with the givenargs, if there is no element with the key in the container.
The constructor of the new element is called with exactly the same arguments as supplied toemplace, forwarded viastd::forward<Args>(args)....The element may be constructed even if there already is an element with the key in the container, in which case the newly constructed element will be destroyed immediately .
Ifvalue_type is notEmplaceConstructible intoset fromargs, the behavior is undefined.
No iterators or references are invalidated.
Contents |
| args | - | arguments to forward to the constructor of the element |
A pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and abool value set totrue if and only if the insertion took place.
If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).
Logarithmic in the size of the container.
Careful use ofemplace allows the new element to be constructed while avoiding unnecessary copy or move operations.
#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);}}; constexprint nof_operations{101}; std::size_t set_emplace(){std::set<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::set<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=""){constauto start=std::chrono::system_clock::now();constauto the_size= set_test();constauto stop=std::chrono::system_clock::now();conststd::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");}
Possible output:
630.58ms for cache warming...577.16ms for insert560.84ms for insert547.10ms for emplace549.44ms for emplace
(C++11) | constructs elements in-place using a hint (public member function)[edit] |
| inserts elementsor nodes(since C++17) (public member function)[edit] |