C++ forward_list 库 - emplace_front() 函数



描述

C++ 函数std::forward_list::emplace_front()在 forward_list 的开头构造并插入新元素,并将列表的大小增加一。

声明

以下是来自 std::forward_list 头文件的 std::forward_list::emplace_front() 函数的声明。

C++11

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

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

参数

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

返回值

异常

如果重新分配失败bad_alloc异常将被抛出。

时间复杂度

常数,即 O(1)

示例

以下示例演示了 std::forward_list::emplace_front() 函数的使用。

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

   forward_list<int> fl = {2, 3, 4, 5};

   fl.emplace_front(1);

   cout << "List contains following elements" << endl;

   for (auto it = fl.begin(); it != fl.end(); ++it)
      cout << *it << endl;

   return 0;
}

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

List contains following elements
1
2
3
4
5
forward_list.htm
广告