C++ forward_list 庫 - pop_front() 函式



描述

C++ 函式std::forward_list::pop_front() 從 forward_list 中移除第一個元素,並將 forward_list 的大小減一。

宣告

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

C++11

void pop_front();

引數

返回值

異常

此成員函式從不丟擲異常。在空 forward_list 上呼叫此函式會導致未定義的行為。

時間複雜度

常數,即 O(1)

示例

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

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

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

   cout << "List contains following elements before" 
      " pop_front operation" << endl;

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

   fl.pop_front();
   fl.pop_front();

   cout << "List contains following elements after" 
      " pop_front operation" << endl;

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

   return 0;
}

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

List contains following elements before pop_front operation
1
2
3
4
5
List contains following elements after pop_front operation
3
4
5
forward_list.htm
廣告

© . All rights reserved.