当前位置:网站首页>浅学Vector---如何使用常见的接口
浅学Vector---如何使用常见的接口
2022-06-21 20:23:00 【念旧~】
什么是vector
Vectors are sequence containers representing arrays that can change in size.
vector是表示可变大小数组的序列容器。
简而言之: vector就是一个可变大小的数组
构造函数,拷贝构造,赋值重载
vector<int> v1; //空的
vector<int> v2(10, 5); //10个5
vector<int> v3(v2.begin(), v2.end()); //用v2的迭代器去初始化
int arr[5] = {
1,2,3,4,5 };
vector<int> v4(arr, arr + 5); //迭代器类似于指针的东西,这样也是没问题的
v1 = v4; //赋值重载
如何遍历?
void test2()
{
vector<int> v1;
v1.push_back(1); //尾查接口,就不多说了
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(5);
v1.push_back(6);
v1.push_back(7);
v1.push_back(8);
v1.push_back(9);
v1.push_back(10);
//下标遍历 利用operator[]的重载
for (size_t i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
//范围for
for (auto& e : v1)
{
cout << e << " ";
}
cout << endl;
//迭代器
//vector<int>::iterator it = v1.begin();
auto it = v1.begin(); //偷一波懒,直接利用auto去推到
while (it != v1.end())
{
cout << *it << " ";
it++;
}
cout << endl;
}
还想要再啰嗦一下迭代器
关于容量的接口,比较简单,就浅说一下


增删查改接口以及迭代器失效的各种情况


尾插尾删非常的简单。直接来到insert和erase,并说一说迭代器失效的问题
insert的接口
vector<int> v1;
v1.insert(v1.begin(), 5); //在begin() 的位置插入5
v1.insert(v1.begin(), 4);
v1.insert(v1.begin(), 3);
v1.insert(v1.begin(), 2);
v1.insert(v1.begin(), 1);
v1.insert(v1.begin(), 5, -1); //在begin()的位置插入5个-1
vector<int> v2(10, 100); //创建v2并给元素10个100
v1.insert(v1.begin(), v2.begin(), v2.end()); //在begin的位置插入v2
erase和find的接口,比较简单就不多说
iterator erase (iterator position); //删除迭代器位置的元素
iterator erase (iterator first, iterator last); //删除两个迭代器范围之间的元素 *前闭后开*
template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);
//在迭代器first和last之间查找val,返回val所在的迭代器
关于迭代器失效的问题
下面这段代码,你运行一下就会发现程序崩溃了
什么原因呢?
void test5()
{
vector<int> v = {
1,2,3,4};
for (auto& e : v)
{
cout << e << " ";
}
cout << endl;
//vector<int>::iterator rit = find(v.begin(), v.end(), 3);
auto it = find(v.begin(), v.end(), 3); //前闭后开
if (it != v.end()) //find函数没有找到返回end()位置
{
v.insert(it, 400);
}
v.erase(it); //删除3
for (auto& e : v)
{
cout << e << " ";
}
}

有好的解决方法吗?
1,重新用find()去找
2,借助erase()和insert()的返回值

用vector练练题
力扣:118, 杨辉三角
class Solution {
public:
vector<vector<int>> generate(int numRows)
{
vector<vector<int>> vv;
vv.resize(numRows); //几行几个数组
for(int i = 0; i < numRows; i++)
{
//第n行,要n个元素的空间
vv[i].resize(i+1); // 给每一个数组开空间
}
for(int i = 0; i < numRows; i++)
{
vv[i][0] = 1; // 直接把每一行第一个给1
vv[i][i] = 1; // 直接把没一行最后一个给1
for(int j = 1; j <= i-1; j++)
{
//规律
vv[i][j] = vv[i-1][j] + vv[i-1][j-1];
}
}
return vv;
}
};
力扣136—比较简单,不多说
26,删除有序数组中的重复项
使用前后指针,在前后不相等的情况下停下来
class Solution {
public:
int removeDuplicates(vector<int>& nums)
{
int i = 0, j = 0;
int len = nums.size();
while( i < len)
{
nums[j++] = nums[i];
while(i+1 < len && nums[i] == nums[i+1])
{
i++;
}
i++;
}
return j;
}
};
力扣137:
给你一个整数数组 nums ,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。
请你找出并返回那个只出现了一次的元素。
class Solution {
public:
int singleNumber(vector<int>& nums)
{
int ret = 0;
//1,使用位运算,总有三个数相同
//那三个数相加,他们对用的bit位肯定能整除3
//如果其中一个bit位不能整除3,那肯定那唯一的数参与
for(size_t i = 0; i < 32; i++)
{
int tmp = 0;
for(const auto& e : nums)
{
tmp += ((e >> i) & 1); // 所有数相加一个bit位的位置
}
if(tmp % 3 != 0) //如果其中一个bit位不能整除3,那肯定那唯一的数参与
{
ret |= 1 << i;
}
}
return ret;
}
};
边栏推荐
- 五分钟带你了解云原生
- 洛谷P1608 路径统计 题解
- Barbell strategy -- extreme stability and extreme waves
- 阿里云容器服务负责人易立:云原生如何解企业降本提效难题?
- MitoZ|Multi-Kmer mode
- 小程序怎样关联微信小程序二维码,实现二码合一聚合
- How to associate the QR code of wechat applet to realize the integration of two codes
- How to check the duplicate of an English paper?
- Background and specificity of Worthington elastase
- How to search foreign literature efficiently?
猜你喜欢

华为鸿蒙开发第三课

What are the authoritative websites that search English documents like CNKI?

Fu · new life, chain · future! The conference on enabling innovation and development of urban chain technology industry was held grandly

弗吉尼亚大学:Ingy ElSayed-Aly | 多智能体强化学习中的基于逻辑的奖励形成

MySQL创建表时的条件有哪些

阿里云容器服务负责人易立:云原生如何解企业降本提效难题?

How to check the duplicate of an English paper?

电子招标采购商城系统:优化传统采购业务,提速企业数字化升级

使用StreamAPI 断言组合,结合本地缓存做模糊查询(比mysql效率提升近1000倍)

PAML|计算dN/dS值的生信软件
随机推荐
Advanced packaging, the beginning of a big cycle -- a symposium on semiconductor equipment
What are the authoritative websites that search English documents like CNKI?
线粒体基因组常见缩写与术语
DateGridView首列排序
As we media, why can some people earn more than 10000 yuan a month, but you can only earn a few yuan a month?
PAML|计算dN/dS值的生信软件
P6758 [BalticOI2013] Vim
Spark 离线开发框架设计与实现
洛谷P1514 [NOIP2010 提高组] 引水入城 题解
大不列颠泰迪熊加入PUBG 手游
棋牌类游戏
Revenue and profit "ebb and flow", water drops turn in pain
Using streamapi assertion combination and local cache for fuzzy query (nearly 1000 times more efficient than MySQL)
Supplier management system of digital commerce cloud Paper Group: promote enterprise information construction and comprehensively improve supplier management efficiency
Worthington胰蛋白酶解决方案
#16迭代器经典案例
Summary of internship interview experience of more than ten failed internship positions
Enzo高灵敏度检测——Arg8-Vasopressin ELISA kit
三维度八视图
Is the security of the securities account given by digging money safe? Can I open an account?