网站首页 > 基础教程 正文
简介:
vectro的数据安排以及操作方式,与array数组非常相似。
array是静态空间,一旦配置了就不能改变;
vector是动态空间,随着元素的加入,它的内部机制会自行扩充空间以容纳新元素。
vector维护的是一个连续性空间,所以vector支持随机存取,可以像数组一样使用"[]"。
vector的数据结构:
vector的erase:
vector的插入操作:
注意:插入数据动态扩容的大小不一定是放大一倍,放大的数为:max(old_size,n)
通过下面的示例可以看出vector在插入数据时capacity的变化规律(IDE:visual studio 2022):
vector的扩容操作:
vector的扩容方式有两种,下面详细分析一下他们的差异。
①vector的resize:
既分配了空间,也创建了对象,会调用构造函数。
void resize(size_type new_size, const T& x) {
if (new_size < size())
erase(begin() + new_size, end());
else
insert(end(), new_size - size(), x);
}
void resize(size_type new_size) { resize(new_size, T()); }
由此可以看出:resize()既修改capacity大小,也修改size大小。
②vector的reserve:
reserve()表示容器预留空间,但不是真正的创建对象,需要通过insert()或push_back()等操作创建对象。
template <class ForwardIterator>
iterator allocate_and_copy(size_type n,
ForwardIterator first, ForwardIterator last) {
iterator result = data_allocator::allocate(n);//创建n个元素的新空间
__STL_TRY {
uninitialized_copy(first, last, result);//把vector旧空间中的所有有效数据(las-first条数据),顺序拷贝到vector的新空间里,
return result;
}
__STL_UNWIND(data_allocator::deallocate(result, n));
}
void reserve(size_type n) {
if (capacity() < n) {
const size_type old_size = size();
iterator tmp = allocate_and_copy(n, start, finish);
destroy(start, finish);//析构vector旧空间中的所有有效数据(las-first条数据)
deallocate();//释放vector旧空间
start = tmp;
finish = tmp + old_size;//vector的实际元素个数还是和以前一样,没有变化
end_of_storage = start + n;//vector总容积大小增加了n个元素
}
}
由此可以看出:reserve()只修改capacity大小,不修改size大小。
通过下面的示例可以看出vector在resize和reserve处理下的差异(IDE:visual studio 2022):
因此,在只需要足够的空间而不需要生成对象时(通常是为了节约插入元素的时间),应该选择reserve方法进行扩容。
示例代码:
#include<iostream>
#include <vector>
using namespace std;
void fun_vector_test()
{
//构造数据
vector<int> ver_obj;
cout << " vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
cout << "插入数据:" << endl;
ver_obj.push_back(5); cout << "push_back(5) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(10); cout << "push_back(10) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(15); cout << "push_back(15) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(20); cout << "push_back(20) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(25); cout << "push_back(25) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(30); cout << "push_back(30) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(35); cout << "push_back(35) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(40); cout << "push_back(40) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(45); cout << "push_back(45) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(50); cout << "push_back(50) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(55); cout << "push_back(55) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(60); cout << "push_back(60) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(65); cout << "push_back(65) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(70); cout << "push_back(70) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(75); cout << "push_back(75) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(80); cout << "push_back(80) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(85); cout << "push_back(85) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(90); cout << "push_back(90) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(95); cout << "push_back(95) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(100); cout << "push_back(100) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(101); cout << "push_back(101) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(102); cout << "push_back(102) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(103); cout << "push_back(103) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(104); cout << "push_back(104) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(105); cout << "push_back(105) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(106); cout << "push_back(106) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(107); cout << "push_back(107) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(108); cout << "push_back(108) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(109); cout << "push_back(109) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
ver_obj.push_back(200); cout << "push_back(200) vector 实际元素个数:" << ver_obj.size() << " vector bucket的总数capacity:" << ver_obj.capacity() << endl;
cout <<"打印信息:" << endl;
auto iter_print = ver_obj.begin();
for (; iter_print != ver_obj.end(); ++iter_print)
{
cout <<" "<< * iter_print;
}
cout << endl;
ver_obj.reserve(10); cout << " vector 调整reserve为10 capacity:" << ver_obj.capacity() << " vector 实际元素个数:" << ver_obj.size() << endl;
iter_print = ver_obj.begin();
for (; iter_print != ver_obj.end(); ++iter_print)
{
cout << " " << *iter_print;
}
cout << endl;
try
{
ver_obj.resize(10); cout << " vector 调整resize为10 capacity:" << ver_obj.capacity() << " vector 实际元素个数:" << ver_obj.size() << endl;
}
catch (const std::exception& e)
{
cout << " vector 调整resize为10 exception:" << e.what() << endl;
}
iter_print = ver_obj.begin();
for (; iter_print != ver_obj.end(); ++iter_print)
{
cout << " " << *iter_print;
}
cout << endl;
ver_obj.resize(80); cout << " vector 调整resize为80 capacity:" << ver_obj.capacity() << " vector 实际元素个数:" << ver_obj.size() << endl;
iter_print = ver_obj.begin();
for (; iter_print != ver_obj.end(); ++iter_print)
{
cout << " " << *iter_print;
}
cout << endl;
ver_obj.reserve(100); cout << " vector 调整reserve为100 capacity:" << ver_obj.capacity() << " vector 实际元素个数:" << ver_obj.size() << endl;
iter_print = ver_obj.begin();
for (; iter_print != ver_obj.end(); ++iter_print)
{
cout << " " << *iter_print;
}
cout << endl;
ver_obj.resize(100); cout << " vector 调整resize为100 capacity:" << ver_obj.capacity() << " vector 实际元素个数:" << ver_obj.size() << endl;
iter_print = ver_obj.begin();
for (; iter_print != ver_obj.end(); ++iter_print)
{
cout << " " << *iter_print;
}
cout << endl;
ver_obj.reserve(150); cout << " vector 调整reserve为150 capacity:" << ver_obj.capacity() << " vector 实际元素个数:" << ver_obj.size() << endl;
iter_print = ver_obj.begin();
for (; iter_print != ver_obj.end(); ++iter_print)
{
cout << " " << *iter_print;
}
cout << endl;
}
int main()
{
fun_vector_test();
return 0;
}
原创不易,欢迎点赞、收藏、关注!
- 上一篇: C++常用方法和技巧——vector性能提升技巧
- 下一篇: Vector达芬奇工具使用笔记
猜你喜欢
- 2024-12-06 Java中Vector和ArrayList之间的区别
- 2024-12-06 4.Python趣味数学笔记:向量运算
- 2024-12-06 高中数学——平面向量
- 2024-12-06 高中数学:向量垂直、共线在解析几何中的应用
- 2024-12-06 复数在高中数学中的应用
- 2024-12-06 不一样的余弦定理
- 2024-12-06 9.理解点和向量的基本操作
- 2024-12-06 深入理解:推荐系统中的召回与排序(一)
- 2024-12-06 空间向量的基本概念、空间向量的数乘运算(小知识点)
- 2024-12-06 高等数学之向量代数与空间解析几何知识点与题型总结
- 最近发表
- 标签列表
-
- gitpush (61)
- pythonif (68)
- location.href (57)
- tail-f (57)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- css3动画 (57)
- c#event (59)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- exec命令 (59)
- canvasfilltext (58)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- node教程 (59)
- console.table (62)
- c++time_t (58)
- phpcookie (58)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)