当前位置:网站首页>Failure of vector insertion element iterator in STL
Failure of vector insertion element iterator in STL
2022-07-03 13:53:00 【yolo_ yyh】
Catalog
One 、capaticy() When the capacity is insufficient , Tail-insert element
Two 、capaticy() When the capacity is sufficient , Tail-insert element
3、 ... and 、capacity() When the capacity is sufficient , Insert the element in the middle
STL in vector Memory is managed by three pointers :_M_start,_M_finish,_M_end_of_storage, All about address , These three pointers are required for operations such as capacity size .
STL The use of these three pointers in the source code is as follows :
iterator begin() _GLIBCXX_NOEXCEPT
{ return iterator(this->_M_impl._M_start); }
iterator end() _GLIBCXX_NOEXCEPT
{ return iterator(this->_M_impl._M_finish); }
reverse_iterator rbegin() noexcept
{ return reverse_iterator(end()); }
reverse_iterator rend() noexcept
{ return reverse_iterator(begin()); }
size_type size() const _GLIBCXX_NOEXCEPT
{ return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
size_type capacity() const _GLIBCXX_NOEXCEPT
{ return size_type(this->_M_impl._M_end_of_storage - this->_M_impl._M_start); }
bool empty() const _GLIBCXX_NOEXCEPT
{ return begin() == end(); }
vector In addition to supporting ordinary iterators , Reverse iterators are also supported , The relationship between the iterator and the three pointers is shown in the following figure :

Take the ordinary forward iterator as an example ,vector Iterators have many things to pay attention to .
One 、capaticy() When the capacity is insufficient , Tail-insert element
The code is as follows :
#include <iostream>
#include <vector>
using namespace std;
void Print(vector<int>& vec) {
cout << "size: " << vec.size() << endl;
cout << "capacity: " << vec.capacity() << endl;
auto it = vec.begin();
cout << "iterator: ";
while(it != vec.end()) {
cout << *it << "(" << &(*it) << ")"
<< "\t";
++it;
}
cout << *it << "(" << &(*it) << ")" << endl;
}
int main() {
vector<int> vec{1,2};
Print(vec);
vec.push_back(3);
Print(vec);
return 0;
}The print before and after inserting the element is as follows :

Conclusion 1 :
capacity() When it's not enough , Capacity expansion will be carried out , All previous iterators will fail .
Two 、capaticy() When the capacity is sufficient , Tail-insert element
#include <iostream>
#include <vector>
using namespace std;
void Print(vector<int>& vec) {
cout << "size: " << vec.size() << endl;
cout << "capacity: " << vec.capacity() << endl;
auto it = vec.begin();
cout << "iterator: ";
while(it != vec.end()) {
cout << *it << "(" << &(*it) << ")"
<< "\t";
++it;
}
cout << *it << "(" << &(*it) << ")" << endl;
}
int main() {
vector<int> vec{1,2};
Print(vec);
vec.push_back(3);
Print(vec);
vec.push_back(4);
Print(vec);
return 0;
}The print before and after inserting the element is as follows :

Conclusion two :
capacity() Insert elements at the tail when the capacity is sufficient , Before vector None of the iterators have failed .
3、 ... and 、capacity() When the capacity is sufficient , Insert the element in the middle
The code is as follows :
#include <iostream>
#include <vector>
using namespace std;
void Print(vector<int>& vec) {
cout << "size: " << vec.size() << endl;
cout << "capacity: " << vec.capacity() << endl;
auto it = vec.begin();
cout << "iterator: ";
while(it != vec.end()) {
cout << *it << "(" << &(*it) << ")"
<< "\t";
++it;
}
cout << *it << "(" << &(*it) << ")" << endl;
}
int main() {
vector<int> vec{1,2};
Print(vec);
vec.push_back(3);
Print(vec);
auto it1 = vec.begin();
++it1;
vec.insert(it1, 4);
Print(vec);
return 0;
}The information printed before and after insertion is as follows :

Conclusion three :
capacity() When the capacity is sufficient , Insert the element in the middle , Iterators before the insertion position are not affected , Later iterators will fail .
边栏推荐
- PHP maze game
- Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
- Stack application (balancer)
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- 研发团队资源成本优化实践
- Several common optimization methods matlab principle and depth analysis
- 解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)
- Go language unit test 4: go language uses gomonkey to test functions or methods
- [quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
- 【556. 下一个更大元素 III】
猜你喜欢

从零开始的基于百度大脑EasyData的多人协同数据标注

【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
![[技术发展-24]:现有物联网通信技术特点](/img/f3/a219fe8e7438b8974d2226b4c3d4a4.png)
[技术发展-24]:现有物联网通信技术特点

Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases

Bidirectional linked list (we only need to pay attention to insert and delete functions)

Go: send the get request and parse the return JSON (go1.16.4)

Flutter动态化 | Fair 2.5.0 新版本特性

Qt学习24 布局管理器(三)
![[redis] cache warm-up, cache avalanche and cache breakdown](/img/df/81f38087704de36946b470f68e8004.jpg)
[redis] cache warm-up, cache avalanche and cache breakdown

全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场
随机推荐
JVM family - overview, program counter day1-1
Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu
Unity embeddedbrowser browser plug-in event communication
Libuv Library - Design Overview (Chinese version)
The solution of Chinese font garbled code in keil5
解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)
Brief analysis of tensorboard visual processing cases
常见的几种最优化方法Matlab原理和深度分析
Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
Kivy tutorial how to load kV file design interface by string (tutorial includes source code)
Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
php 迷宫游戏
JSON serialization case summary
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
Logback log sorting
Replace the GPU card number when pytorch loads the historical model, map_ Location settings
ThreadPoolExecutor realizes multi-threaded concurrency and obtains the return value (elegant and concise way)
3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)
Go language web development series 30: gin: grouping by version for routing
Kivy tutorial how to automatically load kV files