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

边栏推荐
- 2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
- STM32 general timer 1s delay to realize LED flashing
- 01仿B站项目业务架构
- LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
- STM32 general timer output PWM control steering gear
- Installation and removal of MySQL under Windows
- Simple use of MySQL (addition, deletion, modification and query)
- QT detection card reader analog keyboard input
- 学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
- 2. Elment UI date selector formatting problem
猜你喜欢
![[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer](/img/90/4de927e797ec9c2bb70e507392bed0.jpg)
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer

Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn

LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*

openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹

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

The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving

QT self drawing button with bubbles

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

单片机学到什么程度能找到工作,这个标准不好量化

Pymssql controls SQL for Chinese queries
随机推荐
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
In third tier cities and counties, it is difficult to get 10K after graduation
Serial port programming
Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
About windows and layout
LeetCode - 919. 完全二叉树插入器 (数组)
Basic knowledge of communication interface
Idea remote breakpoint debugging jar package project
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
Modelcheckpoint auto save model
I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first
要选择那种语言为单片机编写程序呢
The data read by pandas is saved to the MySQL database
2.Elment Ui 日期选择器 格式化问题
2021-01-03
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
el-table X轴方向(横向)滚动条默认滑到右边
I think all friends should know that the basic law of learning is: from easy to difficult
Oracle database SQL statement execution plan, statement tracking and optimization instance