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);

引數

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
廣告

© . All rights reserved.