跳到主要内容

std::vector emplace_back() 方法

// prism-push-types:reference
// Non const version only
template< class... Args >
constexpr reference emplace_back( Args&&... args );

将一个新元素添加到容器的末尾。该元素通过 std::allocator_traits<Alloc>::construct() 构造,该方法通常使用 placement-new 在容器提供的位置就地构造元素。参数 args...std::forward<Args>(args)... 的形式转发给构造函数。

失效

如果新的 size() 大于 capacity(),则所有迭代器引用(包括尾后迭代器)都将失效。

否则,只有尾后迭代器会失效。

参数

  • args - 转发给元素构造函数的参数

类型要求

返回值

对插入元素的引用。

复杂度

分摊常数 - O(1)

异常

如果抛出异常,此函数不产生任何效果(强异常保证)。

如果 T 的移动构造函数不是 noexceptT 无法 CopyInsertable*this 中,则 vector 将使用抛出异常的移动构造函数。如果它抛出异常,则保证失效,并且效果未指定。

备注

由于可能发生重新分配,emplace_back() 要求 vector 的元素类型为 MoveInsertable

示例

Main.cpp
#include <vector>
#include <string>
#include <cassert>
#include <iostream>

struct President
{
std::string name;
std::string country;
int year;

President(std::string p_name, std::string p_country, int p_year)
: name(std::move(p_name)), country(std::move(p_country)), year(p_year)
{
std::cout << "I am being constructed.\n";
}
President(President&& other)
: name(std::move(other.name)), country(std::move(other.country)), year(other.year)
{
std::cout << "I am being moved.\n";
}
President& operator=(const President& other) = default;
};

int main()
{
std::vector<President> elections;
std::cout << "emplace_back:\n";
auto& ref = elections.emplace_back("Nelson Mandela", "South Africa", 1994);
assert(ref.year == 1994 && "uses a reference to the created object (C++17)");

std::vector<President> reElections;
std::cout << "\npush_back:\n";
reElections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936));

std::cout << "\nContents:\n";
for (President const& president: elections) {
std::cout << president.name << " was elected president of "
<< president.country << " in " << president.year << ".\n";
}
for (President const& president: reElections) {
std::cout << president.name << " was re-elected president of "
<< president.country << " in " << president.year << ".\n";
}
}
输出
emplace_back:
I am being constructed.

push_back:
I am being constructed.
I am being moved.

Contents:
Nelson Mandela was elected president of South Africa in 1994.
Franklin Delano Roosevelt was re-elected president of the USA in 1936.
本文源自此 CppReference 页面。它可能经过修改以进行改进或满足编辑偏好。点击“编辑此页面”可查看此文档的所有更改。
悬停查看原始许可证。

std::vector emplace_back() 方法

// prism-push-types:reference
// Non const version only
template< class... Args >
constexpr reference emplace_back( Args&&... args );

将一个新元素添加到容器的末尾。该元素通过 std::allocator_traits<Alloc>::construct() 构造,该方法通常使用 placement-new 在容器提供的位置就地构造元素。参数 args...std::forward<Args>(args)... 的形式转发给构造函数。

失效

如果新的 size() 大于 capacity(),则所有迭代器引用(包括尾后迭代器)都将失效。

否则,只有尾后迭代器会失效。

参数

  • args - 转发给元素构造函数的参数

类型要求

返回值

对插入元素的引用。

复杂度

分摊常数 - O(1)

异常

如果抛出异常,此函数不产生任何效果(强异常保证)。

如果 T 的移动构造函数不是 noexceptT 无法 CopyInsertable*this 中,则 vector 将使用抛出异常的移动构造函数。如果它抛出异常,则保证失效,并且效果未指定。

备注

由于可能发生重新分配,emplace_back() 要求 vector 的元素类型为 MoveInsertable

示例

Main.cpp
#include <vector>
#include <string>
#include <cassert>
#include <iostream>

struct President
{
std::string name;
std::string country;
int year;

President(std::string p_name, std::string p_country, int p_year)
: name(std::move(p_name)), country(std::move(p_country)), year(p_year)
{
std::cout << "I am being constructed.\n";
}
President(President&& other)
: name(std::move(other.name)), country(std::move(other.country)), year(other.year)
{
std::cout << "I am being moved.\n";
}
President& operator=(const President& other) = default;
};

int main()
{
std::vector<President> elections;
std::cout << "emplace_back:\n";
auto& ref = elections.emplace_back("Nelson Mandela", "South Africa", 1994);
assert(ref.year == 1994 && "uses a reference to the created object (C++17)");

std::vector<President> reElections;
std::cout << "\npush_back:\n";
reElections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936));

std::cout << "\nContents:\n";
for (President const& president: elections) {
std::cout << president.name << " was elected president of "
<< president.country << " in " << president.year << ".\n";
}
for (President const& president: reElections) {
std::cout << president.name << " was re-elected president of "
<< president.country << " in " << president.year << ".\n";
}
}
输出
emplace_back:
I am being constructed.

push_back:
I am being constructed.
I am being moved.

Contents:
Nelson Mandela was elected president of South Africa in 1994.
Franklin Delano Roosevelt was re-elected president of the USA in 1936.
本文源自此 CppReference 页面。它可能经过修改以进行改进或满足编辑偏好。点击“编辑此页面”可查看此文档的所有更改。
悬停查看原始许可证。