目录

forward_list::push_front

描述 (Description)

C ++函数std::forward_list::push_front()在forward_list的开头插入新元素,并将forward_list的大小增加1。

声明 (Declaration)

以下是std :: forward_list :: push_front()函数形式std :: forward_list头的声明。

C++11

void push_front (const value_type& val);

参数 (Parameters)

val - 要插入forward_list的元素的值。

返回值

没有

异常 (Exceptions)

该成员函数从不抛出异常。

时间复杂

常数即O(1)

例子 (Example)

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

#include <iostream>
#include <forward_list>
using namespace std;
int main(void) {
   forward_list<int> fl;
   for (int i = 0; i < 5; ++i)
      fl.push_front(i + 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
5
4
3
2
1
↑回到顶部↑
WIKI教程 @2018