std::hive<T,Allocator>::emplace, std::hive<T,Allocator>::emplace_hint
From cppreference.com
template< class... Args >
iterator emplace( Args&&... args );
|
(1) | (since C++26) |
template< class... Args >
iterator emplace_hint( const_iterator hint, Args&&... args );
|
(2) | (since C++26) |
Inserts a new element of type T into the hive at unspecified location. The constructor of the new element is called with exactly the same arguments as supplied to emplace, forwarded via std::forward<Args>(args)....
Invalidates the end() iterator.
Parameters
| args... | - | arguments to forward to the constructor of the element |
| hint | - | ignored (exists only for compatibility) |
| Type requirements | ||
-T must be EmplaceConstructible into hive from args.... Otherwise, the behavior is undefined.
| ||
Return value
An iterator that points to the new element.
Complexity
Constant.
Exceptions
1,2) If an exception is thrown, does nothing.
Notes
args... are allowed to directly or indirectly refer to a value in *this.
Example
Run this code
import std;
struct Bee
{
std::string s;
Bee(std::string str) : s(std::move(str))
{
std::println(R"( Constructed, s: "{}")", s);
}
Bee(const Bee& o) : s(o.s)
{
std::println(R"( Copy constructed, s: "{}")", s);
}
Bee(Bee&& o) : s(std::move(o.s))
{
std::println(R"( Move constructed, s: "{}")", s);
}
Bee& operator=(const Bee& other)
{
s = other.s;
std::println(R"( Copy assigned, s: "{}")", s);
return *this;
}
Bee& operator=(Bee&& other)
{
s = std::move(other.s);
std::println(R"( Move assigned, s: "{}")", s);
return *this;
}
};
int main()
{
std::hive<Bee> hive;
std::println("Construct Bee twice:");
Bee two{"two"};
Bee three{"three"};
std::println("Emplace:");
hive.emplace("one");
std::println("Emplace with Bee&:");
hive.emplace(two);
std::println("Emplace with Bee&&:");
hive.emplace(std::move(three));
std::println("Hive: {}", hive | std::views::transform([](const Bee& o){ return o.s; }));
}
Possible output:
Construct Bee twice:
Constructed, s: "two"
Constructed, s: "three"
Emplace:
Constructed, s: "one"
Emplace with Bee&:
Move constructed, s: "two"
Emplace with Bee&&:
Move constructed, s: "three"
Hive: ["one", "two", "three"]
See also
| inserts elements (public member function) |