欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

vector中的front和back函数的赋值操作

程序员文章站 2022-03-23 09:19:24
...

#include <vector>
#include <iostream>

int main() {
	using namespace std;
	vector <int> v1;

	v1.push_back(10);
	v1.push_back(11);

	int& i = v1.back();	//返回v1中的最后一个元素
	const int& ii = v1.front();	//返回第一个元素

	cout << "The last integer of v1 is " << i << endl;
	i--;
	cout << "The next-to-last integer of v1 is " << ii << endl;
}

front函数为返回向量中的第一个元素,back返回最后一个函数。