標籤: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