当前位置:网站首页>The underlying principle of vector
The underlying principle of vector
2022-07-03 10:04:00 【Σίσυφος one thousand and nine hundred】
Investigate vector
One 、 principle : The dynamic array vector Underlying principles and related interview questions - Handsome play with programming

Two 、 Use :




#if 1
int main() {
vector<int> iv(3, 9);
for (auto e : iv) cout << e << endl;
cout << "============================" << endl;
iv.push_back(1);
iv.push_back(2);
iv.push_back(3);
iv.push_back(4);
iv.push_back(7);
iv.push_back(0);
for (auto e : iv) cout << e << endl;
return 0;
}
#endif(2)vector Medium reserve and resize The difference between
reserve Is directly expanded to a certain size , Can reduce the number of times to open up 、 The problem of releasing space ( Optimize push_back), You can improve efficiency , Secondly, it can reduce the problem of copying data many times .reserve It's just a guarantee that vector The size of the space in (capacity) At least the size specified by the parameter n.reserve() There is only one parameter .
resize() You can change the size of the available space , There is also a function to change the default value .capacity The size of the will also change with .resize() Can have multiple parameters .
(3)vector Medium size and capacity The difference between .
size At present vector How many elements are there in (finish – start), and capacity Function indicates how many elements it can hold in the allocated memory (end_of_storage – start).
(4)vector The element type of can be a reference ?
vector The underlying implementation of requires a continuous arrangement of objects , References are not objects , There is no real address , therefore vector Element cannot be of reference type .
(5)vector Iterator failures
When inserting an element into vector in , Due to memory reallocation , So the iterators that point to the original memory all fail .
When you delete an element in a container , The element that the iterator points to has been deleted , Then it also causes the iterator to fail .erase Method returns the next valid iterator , So when we want to delete an element , need it=vec.erase(it);.
(6) Release... Correctly vector Of memory (clear(), swap(), shrink_to_fit())
vec.clear(): Empty content , But don't free memory .
vector().swap(vec): Empty content , And free memory , Want a brand new vector.
vec.shrink_to_fit(): Request the container to lower its capacity and size matching .
vec.clear();vec.shrink_to_fit();: Empty content , And free memory .
(7)vector Why should we expand the capacity with 1.5 Times or 2 Double expansion ?
According to the information we have looked up , Consider the possible waste of heap space , Multiple growth can't be too big , There are two widely used expansion methods , With 2 Double the way to expand , Or with 1.5 Double the way to expand .
With 2 Double the way to expand , As a result, the memory of the next application must be larger than the total memory allocated before , As a result, the previously allocated memory can no longer be used , So it's better to set the multiplier factor to (1,2) Between :

边栏推荐
- Application of external interrupts
- 03 fastjason solves circular references
- (2)接口中新增的方法
- 新系列单片机还延续了STM32产品家族的低电压和节能两大优势
- 2020-08-23
- 2021-10-27
- openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
- My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
- Getting started with JMX, MBean, mxbean, mbeanserver
- 嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
猜你喜欢

学习开发没有捷径,也几乎不存在带路会学的快一些的情况

Opencv note 21 frequency domain filtering

Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving

My notes on intelligent charging pile development (II): overview of system hardware circuit design

2. Elment UI date selector formatting problem

03 fastjason solves circular references

Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction

YOLO_ V1 summary

在三线城市、在县城,很难毕业就拿到10K

openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
随机推荐
2020-08-23
Qcombox style settings
Introduction to chromium embedded framework (CEF)
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
51 MCU tmod and timer configuration
2020-08-23
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
yocto 技術分享第四期:自定義增加軟件包支持
Wireshark use
Simple use of MySQL (addition, deletion, modification and query)
Adaptiveavgpool1d internal implementation
In third tier cities and counties, it is difficult to get 10K after graduation
要選擇那種語言為單片機編寫程序呢
For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
Interruption system of 51 single chip microcomputer
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
is_ power_ of_ 2 judge whether it is a multiple of 2
01 business structure of imitation station B project
LeetCode - 919. 完全二叉树插入器 (数组)