STL vector中的front方法(4)

來源:互聯網
上載者:User

標籤:stl   front   

public member function<vector>std::vector::front
      reference front();const_reference front() const;
Access first element

訪問第一個元素

Returns a reference to the first element in the vector.

返回第一個元素的引用。


Unlike member vector::begin, which returns an iterator to this same element, this function returns a direct reference.

和begin不一樣,begin是返回一個迭代器,而front是返回一個直接引用。



Calling this function on an empty container causes undefined behavior.

對一個空的容器調用該方法將會導致不可預料的結果。


Parametersnone

Return value

A reference to the first element in the vector container.

一個指向該vector容器中第一個元素的引用.


If the vector object is const-qualified, the function returns a const_reference. Otherwise, it returns a reference.

如果該vector是const屬性的,那麼返回值也將是const屬性的,否則,就是返回一個普通的iterator。


Member types reference and const_reference are the reference types to the elements of the container (see vector member types).

返回值的類型都是模版元素定義的類型。即元素類型。


Example
12345678910111213141516171819
// vector::front#include <iostream>#include <vector>int main (){  std::vector<int> myvector;  myvector.push_back(78);  myvector.push_back(16);  // now front equals 78, and back 16  myvector.front() -= myvector.back();  std::cout << "myvector.front() is now " << myvector.front() << ‘\n‘;  return 0;}
Edit & Run


Output:
myvector.front() is now 62

ComplexityConstant.

Iterator validityNo changes.
調用該方法不會使其他迭代器失效。
Data races

The container is accessed (neither the const nor the non-const versions modify the container).

容器可以被訪問(該操作不會修改容器內容)

The reference returned can be used to access or modify elements. Concurrently accessing or modifying different elements is safe.

該引用可以用來訪問或者是修改元素,並且他們都是安全的。


Exception safety

If the container is not empty, the function never throws exceptions (no-throw guarantee).

如果容器非空,該方法不會拋出異常。


Otherwise, it causes undefined behavior.

否則,會導致未知的行為!


//翻譯的不好的地方麻煩大家指出來以便修改,謝謝。

//轉載請註明出處http://blog.csdn.net/qq844352155/article/details/38458047

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.