C++ forward_list 庫 - front() 函式



描述

C++ 函式std::forward_list::front() 返回對 forward_list 的第一個元素的引用。

宣告

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

C++11

reference front();
const_reference front() const;

引數

返回值

如果物件是常量限定的,則返回常量引用;否則返回非常量引用。

異常

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

時間複雜度

常數,即 O(1)

示例

以下示例顯示了 std::forward_list::front() 函式的使用方法。

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

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

   cout << "First element of the list = " << fl.front() << endl;

   return 0;
}

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

First element of the list = 1
forward_list.htm
廣告
© . All rights reserved.