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

边栏推荐
- 指定opencv非标准安装的版本
- Redis master-slave and sentinel master-slave switchover are built step by step
- 爬虫实战(七):爬王者英雄图片
- Zhong Xuegao wants to remain innocent in the world
- 杰理之测试盒配置声道【篇】
- R语言使用ggplot2函数可视化需要构建泊松回归模型的计数目标变量的直方图分布并分析构建泊松回归模型的可行性
- 8 CAS
- State mode - Unity (finite state machine)
- Make insurance more "safe"! Kirin Xin'an one cloud multi-core cloud desktop won the bid of China Life Insurance, helping the innovation and development of financial and insurance information technolog
- 我的创作纪念日
猜你喜欢

How to share the same storage among multiple kubernetes clusters

杰理之手动配对方式【篇】

9 atomic operation class 18 Rohan enhancement

Numpy——2.数组的形状

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

2022.07.05
Make this crmeb single merchant wechat mall system popular, so easy to use!

Zhong Xuegao wants to remain innocent in the world

Tips and tricks of image segmentation summarized from 39 Kabul competitions

Redis master-slave and sentinel master-slave switchover are built step by step
随机推荐
Unable to link the remote redis server (solution 100%
tp6 实现佣金排行榜
State mode - Unity (finite state machine)
Solve the problem of remote rviz error reporting
华南X99平台打鸡血教程
位运算介绍
LC: string conversion integer (ATOI) + appearance sequence + longest common prefix
Time tools
Redis master-slave and sentinel master-slave switchover are built step by step
# 欢迎使用Markdown编辑器
Kirin Xin'an won the bid for the new generation dispatching project of State Grid!
R language ggplot2 visualization: use the ggecdf function of ggpubr package to visualize the grouping experience cumulative density distribution function curve, and the linetype parameter to specify t
Matplotlib drawing 3D graphics
PV static creation and dynamic creation
LC:字符串转换整数 (atoi) + 外观数列 + 最长公共前缀
Specify the version of OpenCV non-standard installation
The strength index of specialized and new software development enterprises was released, and Kirin Xin'an was honored on the list
IP tools
L1-027 rental (Lua)
歌单11111