C++ 列表庫 - push_back() 函式



描述

C++ 函式std::list::push_back()在列表的末尾插入新元素,並將列表的大小增加一。

宣告

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

C++11

void push_back (value_type&& val);

引數

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

返回值

無。

異常

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

時間複雜度

常數,即 O(1)

示例

以下示例演示了 std::list::push_back() 函式的使用方法。

#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_back(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
1
2
3
4
5
list.htm
廣告

© . All rights reserved.