Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.
vector::front()
This function can be used to fetch the first element of a vector container.获得vector的第一个元素
Syntax :
vectorname.front() Parameters : No value is needed to pass as the parameter. Returns : Direct reference to the first element of the vector container.
Examples:
Input : myvector = 1, 2, 3 myvector.front(); Output : 1 Input : myvector = 3, 4, 1, 7, 3 myvector.front(); Output : 3
Errors and Exceptions
1. If the vector container is empty, it causes undefined behaviour. 如果vector为空,导致未定义行为
2. It has a no exception throw guarantee if the vector is not empty.
// CPP program to illustrate
// Implementation of front() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> myvector;
myvector.push_back(3);
myvector.push_back(4);
myvector.push_back(1);
myvector.push_back(7);
myvector.push_back(3);
// Ve