当前位置:网站首页>【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语言参考代码:

边栏推荐
- R language ggplot2 visualization: use the ggqqplot function of ggpubr package to visualize the QQ graph (Quantitative quantitative plot)
- AI writes a poem
- L1-019 who falls first (Lua)
- 最长公共前缀(leetcode题14)
- 我的创作纪念日
- 开源OA开发平台:合同管理使用手册
- tp6 实现佣金排行榜
- LC:字符串转换整数 (atoi) + 外观数列 + 最长公共前缀
- Former richest man, addicted to farming
- ant desgin 多选
猜你喜欢

Introduction to bit operation

Kirin Xin'an with heterogeneous integration cloud financial information and innovation solutions appeared at the 15th Hunan Financial Technology Exchange Conference

PV static creation and dynamic creation
让这个 CRMEB 单商户微信商城系统火起来,太好用了!

5billion, another master fund was born in Fujian

Redis master-slave and sentinel master-slave switchover are built step by step

The strength index of specialized and new software development enterprises was released, and Kirin Xin'an was honored on the list

华南X99平台打鸡血教程

PMP每日一练 | 考试不迷路-7.7

CMD command enters MySQL times service name or command error (fool teaching)
随机推荐
The strength index of specialized and new software development enterprises was released, and Kirin Xin'an was honored on the list
Ucloud is a basic cloud computing service provider
Browse the purpose of point setting
杰理之发起对耳配对、回连、开启可发现、可连接的轮循函数【篇】
Numpy——2.数组的形状
Visual Studio 插件之CodeMaid自动整理代码
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the dot strip plot, set the position parameter, and configure the separation degree of different grouped
Experiment 1 of Compilation Principle: automatic implementation of lexical analyzer (Lex lexical analysis)
开源OA开发平台:合同管理使用手册
“本真”是什么意思
Jerry's headphones with the same channel are not allowed to pair [article]
怎么在手机上买股票开户 股票开户安全吗
Tp6 realize Commission ranking
杰理之按键发起配对【篇】
时间工具类
8 CAS
凌云出海记 | 赛盒&华为云:共助跨境电商行业可持续发展
最多可以参加的会议数目[贪心 + 优先队列]
AI写首诗
MySQL、sqlserver oracle数据库连接方式