public member function
<forward_list>

std::forward_list::front

      reference front();const_reference front() const;
訪問第一個元素
返回一個指向 forward_list 容器中第一個元素的引用。

與返回指向同一元素的迭代器的成員 forward_list::begin 不同,此函式返回直接引用。

在空容器上呼叫此函式會導致未定義行為。

引數



返回值

指向容器中第一個元素的引用。

如果 forward_list 物件是 const 限定的,則函式返回一個const_reference。否則,它返回一個引用.

成員型別引用const_reference是 forward_list 容器元素的引用型別(參見 member types)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// forward_list::front
#include <iostream>
#include <forward_list>

int main ()
{
  std::forward_list<int> mylist = {2, 16, 77};

  mylist.front() = 11;

  std::cout << "mylist now contains:";
  for ( int& x : mylist ) std::cout << ' ' << x;

  std::cout << '\n';

  return 0;
}

輸出
mylist now contains: 11 16 77


複雜度

常量。

迭代器有效性

沒有變化。

資料競爭

訪問容器(const 和非 const 版本都不會修改容器)。
第一個元素可以被呼叫者訪問或修改。同時訪問或修改其他元素是安全的。

異常安全

如果容器非空,則該函式永遠不會丟擲異常(no-throw guarantee)。
否則,將導致未定義行為

另見