C++ 列表库 - push_front() 函数



描述

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

声明

以下是来自`std::list`头文件的`std::list::push_front()`函数声明。

C++11

void push_front (value_type&& val);

参数

val − 要分配给新插入列表的值。

返回值

无。

异常

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

时间复杂度

常数,即 O(1)

示例

以下示例演示了`std::list::push_front()`函数的使用。

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> l1 = {1, 2, 3, 4, 5};
   list<int> l2;

   for (auto it = l1.begin(); it != l1.end(); ++it)
      l2.push_front(move(*it));

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

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

   return 0;
}

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

List l2 contains following elements
5
4
3
2
1
list.htm
广告
© . All rights reserved.