stl vector 函數
C ++ vector :: back()函數 (C++ vector::back() function)
vector::back() is a library function of "vector" header, it is used to access the last element from the vector, it returns a reference to the last element of the vector.
vector :: back()是“ vector”標頭的庫函數,用于訪問矢量的最后一個元素,它返回對矢量的最后一個元素的引用。
Note: To use vector, include <vector> header.
注意:要使用向量,請包含<vector>標頭。
Syntax of vector::back() function
vector :: back()函數的語法
vector::back();
Parameter(s): none – It accepts nothing.
參數: 無 –不接受任何內容。
Return value: reference – It returns a reference to the last element of vector.
返回值: reference –返回對向量的最后一個元素的引用。
Example:
例:
Input:
vector<int> vector1{ 1, 2, 3, 4, 5 };
Function call:
cout << vector1.back() << endl;
Output:
5
C ++程序演示vector :: back()函數的示例 (C++ program to demonstrate example of vector::back() function)
//C++ STL program to demonstrate example of
//vector::back() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1{ 10, 20, 30, 40, 50 };
//accessing last element
//using vector::back() function
cout << "last element is: " << v1.back() << endl;
//changing last element
v1.at(v1.size() - 1) = 100;
cout << "now, last element is: " << v1.back() << endl;
//changing last element
//using push_back()
v1.push_back(200);
cout << "now, last element is: " << v1.back() << endl;
return 0;
}
Output
輸出量
last element is: 50
now, last element is: 100
now, last element is: 200
Reference: C++ vector::back()
參考: C ++ vector :: back()
翻譯自: https://www.includehelp.com/stl/vector-back-function-with-example.aspx
stl vector 函數