当前位置:网站首页>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 :
边栏推荐
- MySQL root user needs sudo login
- YOLO_ V1 summary
- JS foundation - prototype prototype chain and macro task / micro task / event mechanism
- 4G module IMEI of charging pile design
- 当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学
- 自动装箱与拆箱了解吗?原理是什么?
- 4G module at command communication package interface designed by charging pile
- 单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
- openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
- 在三线城市、在县城,很难毕业就拿到10K
猜你喜欢
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
Adaptiveavgpool1d internal implementation
Blue Bridge Cup for migrant workers majoring in electronic information engineering
I think all friends should know that the basic law of learning is: from easy to difficult
Notes on C language learning of migrant workers majoring in electronic information engineering
干单片机这一行的时候根本没想过这么多,只想着先挣钱养活自己
随机推荐
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
ADS simulation design of class AB RF power amplifier
Stm32 NVIC interrupt priority management
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
Exception handling of arm
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
(1) 什么是Lambda表达式
Sending and interrupt receiving of STM32 serial port
An executable binary file contains more than machine instructions
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
2.Elment Ui 日期选择器 格式化问题
is_ power_ of_ 2 judge whether it is a multiple of 2
Idea remote breakpoint debugging jar package project
2021-10-27
单片机学到什么程度能找到工作,这个标准不好量化
干单片机这一行的时候根本没想过这么多,只想着先挣钱养活自己
Which language should I choose to program for single chip microcomputer
Not many people can finally bring their interests to college graduation
Opencv note 21 frequency domain filtering