LeetCode刷题小结之vector的使用

本文总结了C++中vector的特点,包括其可变长度和存储任意类型数据的能力,并探讨了在LeetCode刷题过程中vector的常见用法和自定义类型存储的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Vector

1.特点:vector是C++标准模板库中的内容,它具有可变长度、可存取任意数据类型的特点,使用vector可以构建类似多维数组的数据结构。

2.常用方法:

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	vector<int> test;

	test.push_back(8);
	test.push_back(5);
	test.push_back(3);
	test.push_back(7); //尾部加入元素

        reverse(test.begin(), test.end()); //反转vector

       for (vector<int>::iterator it = test.begin(); it != test.end(); ++it)
		cout << *it << endl;  //使用迭代器遍历

	for (int i = 0; i < test.size(); ++i)
		cout << test[i] << endl; //使用索引遍历

	cout << test.size() << endl;  //返回v的长度

	test.pop_back(); //删除v最后一个元素,类似于弹栈,因此可用来实现栈

	cout << test.back() << endl; //取最后一个元素

	vector<int>::iterator it = test.begin(); //声明迭代器

	it = test.erase(it); //删除迭代器it指
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值