目录

vector::emplace_back

描述 (Description)

C ++函数std::vector::emplace_back()在向量的末尾插入新元素。 如果需要更多空间,则会重新分配。

此方法将容器大小增加一个。

声明 (Declaration)

以下是std :: vector :: emplace_back()函数形式std :: vector标头的声明。

C++11

template <class... Args>
void emplace_back (Args&&... args);

参数 (Parameters)

args - 转发以构造新元素的参数。

返回值

没有

异常 (Exceptions)

如果重新分配失败,则抛出bad_alloc异常。

时间复杂

常数即O(1)

例子 (Example)

以下示例显示了std :: vector :: emplace_back()函数的用法。

#include <iostream>
#include <vector>
using namespace std;
int main(void) {
   vector<int> v = {1, 2, 3};
   v.emplace_back(4);
   v.emplace_back(5);
   for (auto it = v.begin(); it != v.end(); ++it)
      cout << *it << endl;
   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

1
2
3
4
5
↑回到顶部↑
WIKI教程 @2018