当前位置:网站首页>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 .
边栏推荐
- Golang — template
- Halcon combined with C # to detect surface defects -- Halcon routine autobahn
- Brief analysis of tensorboard visual processing cases
- windos 创建cordova 提示 因为在此系统上禁止运行脚本
- Comprehensive case of MySQL data addition, deletion, modification and query
- KEIL5出现中文字体乱码的解决方法
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- [556. Next larger element III]
- Software testing is so hard to find, only outsourcing offers, should I go?
- Richview trvstyle liststyle list style (bullet number)
猜你喜欢

Libuv Library - Design Overview (Chinese version)

Common network state detection and analysis tools

Mycms we media mall v3.4.1 release, user manual update

Ocean CMS vulnerability - search php

Students who do not understand the code can also send their own token, which is easy to learn BSC
![[sort] bucket sort](/img/52/95514b5a70cea75821883e016d8adf.jpg)
[sort] bucket sort

TensorBoard可视化处理案例简析

Using registered classes to realize specific type matching function template

Halcon combined with C # to detect surface defects -- Halcon routine autobahn

Golang - command line tool Cobra
随机推荐
Mycms we media mall v3.4.1 release, user manual update
Unity render streaming communicates with unity through JS
Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
静态链表(数组的下标代替指针)
Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
MySQL 数据增删改查综合案例
又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
【BW16 应用篇】安信可BW16模组与开发板更新固件烧录说明
Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
windos 创建cordova 提示 因为在此系统上禁止运行脚本
Qt学习19 Qt 中的标准对话框(上)
Unity embeddedbrowser browser plug-in event communication
太阳底下无新事,元宇宙能否更上层楼?
Qt学习18 登录对话框实例分析
Heap structure and heap sort heapify
MapReduce implements matrix multiplication - implementation code
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Solve MySQL 1045 access denied for user 'root' @ 'localhost' (using password: yes)
C language standard IO function sorting