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 函数