vector::front
傳回第一個項目的參考在向量。
reference front( );
const_reference front( ) const;
傳回值
在第一個項目的參考的物件。 如果向量是空的,則傳回未定義。
備註
如果 front 的傳回值指派給 const_reference,無法修改的物件。 如果 front 的傳回值指派給 reference,可以修改的物件。
當以 _SECURE_SCL 1 編譯時,執行時會發生錯誤,如果您嘗試存取在空白向量的項目。 如需詳細資訊,請參閱檢查過的 Iterator。
範例
// vector_front.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
vector <int> v1;
v1.push_back( 10 );
v1.push_back( 11 );
int& i = v1.front( );
const int& ii = v1.front( );
cout << "The first integer of v1 is "<< i << endl;
// by incrementing i, we move the the front reference to the second element
i++;
cout << "Now, the first integer of v1 is "<< i << endl;
}
Output
The first integer of v1 is 10
Now, the first integer of v1 is 11
需求
標題: <vector>
命名空間: std