博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vector学习之reserver和resize操作
阅读量:2193 次
发布时间:2019-05-02

本文共 2639 字,大约阅读时间需要 8 分钟。

reserve预留存储空间,操作vector之前如果设置这个存储空间在插入数据时就节省了每次分配空间的时间resize改变容器可存储元素的个数。

#include 
#include
#include
using namespace std;typedef struct Personal2{ int m_age; int m_height; string m_name; ~Personal2() { //cout << "destructor~~~Personal=========age~" << m_age << endl; }}Person2;//resize, reserve对比使用测试void resize_reserve_Test(){ int count = 33;//214748 int nsize = 55; string name = string("床前明月光,疑似地上霜,举头望明月,低头思故乡。"); vector
vec1; Person2 person; person.m_name = string("床前明月光,疑似地上霜,举头望明月,低头思故乡。"); cout << "resize_reserve_Test==========" <
<< vec1.empty() << endl; vec1.clear(); cout << "end vec1.empty()====" << vec1.empty() << endl; cout << endl; vec1.reserve(count); for(int i = 0; i < count; i++) { person.m_age = i; person.m_height = i; vec1.emplace_back(person); } cout << "vec2.size=" << vec1.size() << " vec2.capacity=" << vec1.capacity() << endl; vec1.clear(); cout << endl; vec1.reserve(nsize);//设置容量为nsize for(int i = 0; i < count; i++) { person.m_age = i; person.m_height = i; vec1.emplace_back(person); } cout << "vec3.size=" << vec1.size() << " vec3.capacity=" << vec1.capacity() << endl; vec1.clear(); cout << endl; for(int i = 0; i < count; i++) { person.m_age = i; person.m_height = i; vec1.emplace_back(person); } cout << "vec4.size=" << vec1.size() << " vec4.capacity=" << vec1.capacity() << endl; vec1.clear(); cout << endl; vector
vec2; vec2.reserve(count); for(int i = 0; i < count; i++) { person.m_age = i; person.m_height = i; vec2.push_back(person); } cout << "vec5.size=" << vec2.size() << " vec5.capacity=" << vec2.capacity() << endl; vec2.clear(); cout << endl; for(int i = 0; i < nsize; i++) { person.m_age = i; person.m_height = i; vec2.push_back(person); } cout << "vec6.size=" << vec2.size() << " vec6.capacity=" << vec2.capacity() << endl; vec2.clear(); cout << endl; for(int i = 0; i < nsize; i++) { person.m_age = i; person.m_height = i; vec2.push_back(person); } vec2.resize(count); cout << "vec7.size=" << vec2.size() << " vec7.capacity=" << vec2.capacity() << endl; vec2.clear(); cout << "vec8.size=" << vec2.size() << " vec8.capacity=" << vec2.capacity() << endl; cout << endl;}int main(){ //int = [-2147483648, 2147483647] resize_reserve_Test(); cout << endl; cout << "Hello World!" << endl; return 0;}

 

通过以上结果我们可知,reserve操作时,如果之前的vec已配了空间,这个时候再分配空间,这个值如果比之前的大,则会分配成功,如果分配的值比之前小,则vector的存储空间还是原来的大小。resize只是改变元素的个数,并不能改变存储空间的大小,

 

转载地址:http://vfiub.baihongyu.com/

你可能感兴趣的文章
检查Linux服务器性能
查看>>
Java 8新的时间日期库
查看>>
Chrome开发者工具
查看>>
【LEETCODE】102-Binary Tree Level Order Traversal
查看>>
【LEETCODE】106-Construct Binary Tree from Inorder and Postorder Traversal
查看>>
【LEETCODE】202-Happy Number
查看>>
和机器学习和计算机视觉相关的数学
查看>>
十个值得一试的开源深度学习框架
查看>>
【LEETCODE】240-Search a 2D Matrix II
查看>>
【LEETCODE】53-Maximum Subarray
查看>>
【LEETCODE】215-Kth Largest Element in an Array
查看>>
【LEETCODE】241-Different Ways to Add Parentheses
查看>>
【LEETCODE】312-Burst Balloons
查看>>
【LEETCODE】232-Implement Queue using Stacks
查看>>
【LEETCODE】225-Implement Stack using Queues
查看>>
【LEETCODE】155-Min Stack
查看>>
【LEETCODE】20-Valid Parentheses
查看>>
【LEETCODE】290-Word Pattern
查看>>
【LEETCODE】36-Valid Sudoku
查看>>
【LEETCODE】205-Isomorphic Strings
查看>>