std::multiset::emplace(3)
π std::multiset::emplace
okay so bestie, welcome to the C++ Standard Libary page for std::multiset::emplace β like, yes, "Libary" is spelled that way in the header, we don't fix it, we just vibe π«Ά. This is section 3 of the manual, and it's literally the glow-up of inserting stuff into a multiset.
πΈ NAME πΈ
std::multiset::emplace β which is, and I cannot stress this enough, std::multiset::emplace π. The name is the name, bestie.
β¨ Synopsis β¨
template< class... Args > (since C++11)
iterator emplace( Args&&... args );
So what's the tea? It inserts a new element into the container, constructed in-place with the given args. In-place. Right there. No travelling, no layovers π§.
The constructor of the new element is called with exactly the same arguments as supplied to emplace, forwarded via std::forward<Args>(args)....
Careful use of emplace allows the new element to be constructed while avoiding unnecessary copy or move operations. That's the whole point, that's the slay, that's the main character energy π.
No iterators or references are invalidated. NONE. Not one. Your iterators stay valid, your references stay valid, everyone's fine, we're all fine π.
π Parameters π
argsβ arguments to forward to the constructor of the element. You hand them over, it forwards them, no questions asked.
π Return value π
An iterator to the inserted element. That's it, that's the gift π β a little pointer to your brand new baby element.
π₯ Exceptions π₯
If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee). Not a partial effect, not a "well, kind of" effect β literally no effect. It's giving all-or-nothing icon behaviour β.
β Complexity β
Logarithmic in the size of the container. Log. N. That's the whole vibe check.
π¦ Example π¦
// Run this code
#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");
}
π Possible output π
499.61ms for cache warming...
447.89ms for insert
436.77ms for insert
430.62ms for emplace
428.61ms for emplace
(notice how emplace is lowkey just a lil faster β that's the whole point of the page, no cap π)
π See also π
emplace_hintβ constructs elements in-place using a hint (C++11) (public member function)insertβ inserts elements or nodes (since C++17) (public member function)
and that's the page, bestie!! π source is http://cppreference.com, dated 2024.06.10, and it's the std::multiset::emplace(3) manual page β periodt. go forth and emplace responsibly π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms