C++ forward_list 庫 - push_front() 函式



描述

C++ 函式std::forward_list::push_front()在 forward_list 的開頭插入新元素,並將 forward_list 的大小增加一。

宣告

以下是來自 std::forward_list 標頭檔案的 std::forward_list::push_front() 函式的宣告。

C++11

void push_front (value_type&& val);

引數

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

返回值

異常

此成員函式從不丟擲異常。

時間複雜度

線性,即 O(n)

示例

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

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

   forward_list<int> fl1 = {1, 2, 3, 4, 5};
   forward_list<int> fl2;

   for (auto it = fl1.begin(); it != fl1.end(); ++it)
      fl2.push_front(move(*it));

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

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

   return 0;
}

讓我們編譯並執行以上程式,這將產生以下結果:

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

© . All rights reserved.