当前位置:网站首页>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 :

边栏推荐
- 学习开发没有捷径,也几乎不存在带路会学的快一些的情况
- Stm32f407 key interrupt
- STM32 general timer output PWM control steering gear
- 03 FastJson 解决循环引用
- STM32 general timer 1s delay to realize LED flashing
- Opencv note 21 frequency domain filtering
- [Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
- Education is a pass and ticket. With it, you can step into a higher-level environment
- The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
- 单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
猜你喜欢

没有多少人能够最终把自己的兴趣带到大学毕业上

Uniapp realizes global sharing of wechat applet and custom sharing button style

要选择那种语言为单片机编写程序呢

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

Adaptiveavgpool1d internal implementation

我想各位朋友都应该知道学习的基本规律就是:从易到难

单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了

I think all friends should know that the basic law of learning is: from easy to difficult

单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇

Swing transformer details-1
随机推荐
新系列单片机还延续了STM32产品家族的低电压和节能两大优势
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
is_ power_ of_ 2 judge whether it is a multiple of 2
getopt_ Typical use of long function
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
(2)接口中新增的方法
ADS simulation design of class AB RF power amplifier
(1) 什么是Lambda表达式
2. Elment UI date selector formatting problem
Circular queue related design and implementation reference 1
yocto 技術分享第四期:自定義增加軟件包支持
QT is a method of batch modifying the style of a certain type of control after naming the control
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
You need to use MySQL in the opening experiment. How can you forget the basic select statement? Remedy is coming~
Retinaface: single stage dense face localization in the wild
Pymssql controls SQL for Chinese queries
Basic knowledge of MySQL database (an introduction to systematization)
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
4G module IMEI of charging pile design