std::hive<T,Allocator>::emplace, std::hive<T,Allocator>::emplace_hint
template< class... Args > iterator emplace( Args&&... args ); | (1) | (с C++26) |
template< class... Args > iterator emplace_hint( const_iterator hint, Args&&... args ); | (2) | (с C++26) |
Вставляет новый элемент типа T в контейнер hive в неопределённую позицию. Конструктор нового элемента вызывается с точно теми же аргументами, что и emplace, переданными с помощью std::forward<Args>(args)....
Инвалидирует итератор end().
Параметры
| args... | - | аргументы, передаваемые конструктору элемента |
| hint | - | игнорируется (существует только для совместимости) |
| Требования к типам | ||
-T должен удовлетворять требованию EmplaceConstructible при создании в hive из args.... В противном случае поведение не определено. |
||
Возвращаемое значение
Итератор, указывающий на новый элемент.
Сложность
Постоянная.
Исключения
1,2) Если выбрасывается исключение, функция ничего не делает.
Примечания
args... могут прямо или косвенно ссылаться на значение в *this.
Пример
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; }));
}
Возможный вывод:
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"]
См. также
| вставляет элементы (открытая функция-член) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/container/hive/emplace