跳到主要内容

std::deque emplace() 方法

// Non const version only
template< class... Args >
iterator emplace( const_iterator pos, Args&&... args );

直接在 pos 之前将新元素插入到容器中。

该元素通过 std::allocator_traits::construct() 构造,该方法通常使用 placement-new 在容器提供的位置就地构造元素。
但是,如果所需位置已被现有元素占用,则插入的元素会先在另一个位置构造,然后移动赋值到所需位置。

参数 args...std::forward<Args>(args)... 的形式转发给构造函数。args... 可以直接或间接引用容器中的值。

失效

所有迭代器,包括past-the-end 迭代器,都将失效。
引用也会失效,除非 pos == begin()pos == end(),在这种情况下它们不会失效。

参数

  • pos - 新元素将在此迭代器之前构造
  • args - 转发给元素构造函数的参数

类型要求

返回值

指向插入元素的迭代器。

复杂度

pos 和容器两端中较短距离呈线性关系 - O(std::min(N, M)),其中

  • Nstd::distance(pos, begin())
  • Mstd::distance(pos, end())

异常

如果除了值类型的拷贝构造函数、移动构造函数、赋值运算符或移动赋值运算符之外抛出异常,或者在使用 emplace 在任一端插入单个元素时抛出异常,则没有影响(强异常保证)。

否则,效果未指定。

示例

Main.cpp
#include <iostream>
#include <string>
#include <deque>

struct A {
std::string s;
A(std::string str) : s(std::move(str)) { std::cout << " constructed\n"; }
A(const A& o) : s(o.s) { std::cout << " copy constructed\n"; }
A(A&& o) : s(std::move(o.s)) { std::cout << " move constructed\n"; }
A& operator=(const A& other) {
s = other.s;
std::cout << " copy assigned\n";
return *this;
}
A& operator=(A&& other) {
s = std::move(other.s);
std::cout << " move assigned\n";
return *this;
}
};

int main()
{
std::deque<A> container;

std::cout << "construct 2 times A:\n";
A two { "two" };
A three { "three" };

std::cout << "emplace:\n";
container.emplace(container.end(), "one");

std::cout << "emplace with A&:\n";
container.emplace(container.end(), two);

std::cout << "emplace with A&&:\n";
container.emplace(container.end(), std::move(three));

std::cout << "content:\n";
for (const auto& obj : container)
std::cout << ' ' << obj.s;
std::cout << '\n';
}
可能输出
construct 2 times A:
constructed
constructed
emplace:
constructed
emplace with A&:
copy constructed
emplace with A&&:
move constructed
content:
one two three
本文源自 此 CppReference 页面。它可能为了改进或编辑偏好而有所改动。单击“编辑此页面”可查看本文档的所有更改。
悬停查看原始许可证。

std::deque emplace() 方法

// Non const version only
template< class... Args >
iterator emplace( const_iterator pos, Args&&... args );

直接在 pos 之前将新元素插入到容器中。

该元素通过 std::allocator_traits::construct() 构造,该方法通常使用 placement-new 在容器提供的位置就地构造元素。
但是,如果所需位置已被现有元素占用,则插入的元素会先在另一个位置构造,然后移动赋值到所需位置。

参数 args...std::forward<Args>(args)... 的形式转发给构造函数。args... 可以直接或间接引用容器中的值。

失效

所有迭代器,包括past-the-end 迭代器,都将失效。
引用也会失效,除非 pos == begin()pos == end(),在这种情况下它们不会失效。

参数

  • pos - 新元素将在此迭代器之前构造
  • args - 转发给元素构造函数的参数

类型要求

返回值

指向插入元素的迭代器。

复杂度

pos 和容器两端中较短距离呈线性关系 - O(std::min(N, M)),其中

  • Nstd::distance(pos, begin())
  • Mstd::distance(pos, end())

异常

如果除了值类型的拷贝构造函数、移动构造函数、赋值运算符或移动赋值运算符之外抛出异常,或者在使用 emplace 在任一端插入单个元素时抛出异常,则没有影响(强异常保证)。

否则,效果未指定。

示例

Main.cpp
#include <iostream>
#include <string>
#include <deque>

struct A {
std::string s;
A(std::string str) : s(std::move(str)) { std::cout << " constructed\n"; }
A(const A& o) : s(o.s) { std::cout << " copy constructed\n"; }
A(A&& o) : s(std::move(o.s)) { std::cout << " move constructed\n"; }
A& operator=(const A& other) {
s = other.s;
std::cout << " copy assigned\n";
return *this;
}
A& operator=(A&& other) {
s = std::move(other.s);
std::cout << " move assigned\n";
return *this;
}
};

int main()
{
std::deque<A> container;

std::cout << "construct 2 times A:\n";
A two { "two" };
A three { "three" };

std::cout << "emplace:\n";
container.emplace(container.end(), "one");

std::cout << "emplace with A&:\n";
container.emplace(container.end(), two);

std::cout << "emplace with A&&:\n";
container.emplace(container.end(), std::move(three));

std::cout << "content:\n";
for (const auto& obj : container)
std::cout << ' ' << obj.s;
std::cout << '\n';
}
可能输出
construct 2 times A:
constructed
constructed
emplace:
constructed
emplace with A&:
copy constructed
emplace with A&&:
move constructed
content:
one two three
本文源自 此 CppReference 页面。它可能为了改进或编辑偏好而有所改动。单击“编辑此页面”可查看本文档的所有更改。
悬停查看原始许可证。