当前位置:网站首页>【STL】vector
【STL】vector
2022-07-07 17:36:00 【Yuucho】
文章目录
1. vector
vector中文意思是向量,它是一个可以改变大小的动态顺序表。
2. vector常用接口
2.1 构造函数
constructor | 接口说明 |
---|---|
vector()(重点) | 无参构造 |
vector (const vector& x); (重点) | 拷贝构造 |
vector(size_type n, const value_type& val = value_type()) | 构造并初始化n个val |
vector (InputIterator first, InputIterator last); | 使用迭代器进行初始化构造 |
vector是一个类模板,它不仅可以存储int、double等类型,还可以存储string,严格来说vector可以存储任意类型。
void test_vector1()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
vector<double> v2;
v2.push_back(1.1);
v2.push_back(2.2);
v2.push_back(3.3);
vector<string> v3;
//单参数的构造函数支持隐式转换
//string(const char* str)
//string s = "hello world";
//常量字符串构造一个临时对象
//再拷贝构造给s
//被优化成直接构造s
v3.push_back("李白");
v3.push_back("杜甫");
v3.push_back("苏轼");
v3.push_back("白居易");
//10个5初始化
vector<int> v4(10, 5);
//迭代器区间初始化
vector<string> v5(++v3.begin(), --v3.end());
string s = "hello world";
vector<char> v6(s.begin(), s.end());
}
测试:
2.2 遍历
void test_vector2()
{
// 遍历
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
// 1、下标+[]
for (size_t i = 0; i < v.size(); ++i)
{
v[i] += 1;
cout << v[i] << " ";
}
cout << endl;
// 2.迭代器
vector<int>::iterator it = v.begin();
while (it != v.end())
{
*it -= 1;
cout << *it << " ";
++it;
}
cout << endl;
// 3.范围for
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
测试:
2.3 增容
void test_vector3()
{
size_t sz;
vector<int> foo;
//当前容量
sz = foo.capacity();
//增容,容量变了就打印一下
cout << "making foo grow:\n";
for (int i = 0; i < 100; ++i)
{
foo.push_back(i);
if (sz != foo.capacity())
{
sz = foo.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}
vs2019下测试:vs(PJ版本STL)下大概是1.5倍的增容。
Linux下:g++(SGI版本STL)是按2倍增长的。
g++运行结果:
making foo grow:
capacity changed: 1
capacity changed: 2
capacity changed: 4
capacity changed: 8
capacity changed: 16
capacity changed: 32
capacity changed: 64
capacity changed: 128
单词增容增多少的分析:
单次增容越多,插入N个值,增容次数越少,效率就越高。
单次增容越多,可能浪费就越多。
单次增少了,会导致频繁增容,效率低下,
所以VS、Linux分别选择1.5倍和2倍增容,这也是平衡过的结果。
如果我们已经知道要使用多大的空间,就可以用reserve提前把空间开好(resize会改变size,插入还是会扩容):
2.4 shrink_to_fit
string、vector都有一个特点:删除数据,一般是不会主动缩容。vector提供shrink_to_fit来请求容器减少容量以适应其大小。
shrink_to_fit的缺点:
(1)如果之后又要插入数据,就又要扩容,那还缩容干嘛!
(2)操作系统不允许内存还一部分,所以缩容的实际操作是先申请一块缩容大小(假设为n个字节)的空间,再把原空间的前n个字节拷贝过来,再释放原空间。
shrink_to_fit付出了性能代价,建议大家慎用。
void test_vector4()
{
size_t sz;
vector<int> foo;
foo.reserve(100);
foo.resize(10);
cout << foo.size() << endl;
cout << foo.capacity() << endl;
// 慎用、少用
foo.shrink_to_fit();
cout << foo.size() << endl;
cout << foo.capacity() << endl;
}
测试:
2.5 insert和erase
vector的insert不支持下标,只支持迭代器。
void test_vector5()
{
// 遍历
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.insert(v.begin(), -1);
v.insert(v.begin(), -2);
v.insert(v.begin(), -3);
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
v.insert(v.begin()+7, 300);
//v.insert(v.begin()+8, 300);越界,非法的迭代器位置
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
v.erase(v.begin());
v.erase(v.begin());
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
2.6 find
vector没有查找函数,但是可以复用algorithm库里的find。
void test_vector6()
{
// 遍历
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
//vector<int>::iterator pos = find(v.begin(), v.end(), 3);
auto pos = find(v.begin(), v.end(), 3);
if (pos != v.end())
{
cout << "找到了" << endl;
v.erase(pos);
}
else
{
cout << "没有找到" << endl;
}
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
2.7 sort
void test_vector7()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(0);
v.push_back(9);
v.push_back(3);
v.push_back(1);
// 默认是升序
sort(v.begin(), v.end());
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
// 排降序,仿函数
// 关于仿函数,大家先记住这个用法,具体我们后面讲队列再详细讲
sort(v.begin(), v.end(), greater<int>()); // >
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
3. 练习题
3.1 杨辉三角
杨辉三角
关于题目给的vector(vector<int>)
:
核心思想:找出杨辉三角的规律,发现每一行头尾都是1,中间第[j]个数等于上一行[j-1]+
[j]
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> vv;
// 先开辟杨辉三角的空间
vv.resize(numRows);
//初始化每一行
for(size_t i = 0; i < numRows; ++i)
{
//每行个数依次递增
vv[i].resize(i+1, 0);
// 每一行的第一个和最后一个都是1
vv[i][0] = 1;
vv[i][vv[i].size()-1] = 1;
}
for(size_t i = 0; i < vv.size(); ++i)
{
for(size_t j = 0; j < vv[i].size(); ++j)
{
if(vv[i][j] == 0)
{
//之间位置等于上一行j-1和j个相加
vv[i][j] = vv[i-1][j-1] + vv[i-1][j];
}
}
}
return vv;
}
};
C语言参考代码:
边栏推荐
- Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!
- 时间工具类
- Throughput
- Command mode - unity
- 干货分享|DevExpress v22.1原版帮助文档下载集合
- 浏览积分设置的目的
- Netease Yunxin participated in the preparation of the standard "real time audio and video service (RTC) basic capability requirements and evaluation methods" issued by the Chinese Academy of Communica
- Pasqal首席技术官:模拟量子计算率先为工业带来量子优势
- L1-019 who falls first (Lua)
- R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置position参数配置不同分组数据点的分离程度
猜你喜欢
爬虫实战(七):爬王者英雄图片
2022.07.04
论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
8 CAS
J ü rgen schmidhub reviews the 25th anniversary of LSTM papers: long short term memory All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversar
Business experience in virtual digital human
让这个 CRMEB 单商户微信商城系统火起来,太好用了!
Command mode - unity
杰理之相同声道的耳机不允许配对【篇】
Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!
随机推荐
LeetCode1051(C#)
PMP每日一练 | 考试不迷路-7.7
PV静态创建和动态创建
谷歌seo外链Backlinks研究工具推荐
杰理之测试盒配置声道【篇】
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的填充色、add参数在小提琴图添加箱图
一锅乱炖,npm、yarn cnpm常用命令合集
Introduction to bit operation
How to buy stocks on your mobile phone and open an account? Is it safe to open an account
R语言ggplot2可视化:使用ggpubr包的ggqqplot函数可视化QQ图(Quantile-Quantile plot)
反爬虫的重点:识别爬虫
R language uses ggplot2 function to visualize the histogram distribution of counting target variables that need to build Poisson regression model, and analyzes the feasibility of building Poisson regr
Redis master-slave and sentinel master-slave switchover are built step by step
IP 工具类
The strength index of specialized and new software development enterprises was released, and Kirin Xin'an was honored on the list
杰理之按键发起配对【篇】
2022.07.05
Netease Yunxin participated in the preparation of the standard "real time audio and video service (RTC) basic capability requirements and evaluation methods" issued by the Chinese Academy of Communica
tp6 实现佣金排行榜
LeetCode 535(C#)