public member function
<list>

std::list::front

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

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

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

引數



返回值

對list容器中第一個元素的引用。

如果 list 物件是 const-qualified,則函式返回一個const_reference。否則,它返回一個引用.

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

示例

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

int main ()
{
  std::list<int> mylist;

  mylist.push_back(77);
  mylist.push_back(22);

  // now front equals 77, and back 22

  mylist.front() -= mylist.back();

  std::cout << "mylist.front() is now " << mylist.front() << '\n';

  return 0;
}

輸出
mylist.front() is now 55


複雜度

常量。

迭代器有效性

沒有變化。

資料競爭

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

異常安全

如果容器不為空,則該函式永遠不會丟擲異常(無異常保證)。
否則,將導致未定義行為

另見