当前位置:网站首页>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 .
边栏推荐
- Can newly graduated European college students get an offer from a major Internet company in the United States?
- 【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
- Summary of common error reporting problems and positioning methods of thrift
- PHP maze game
- Another industry has been broken by Chinese chips. No wonder the leading analog chip companies in the United States have cut prices and sold off
- [技術發展-24]:現有物聯網通信技術特點
- Swiftui development experience: the five most powerful principles that a programmer needs to master
- 项目协作的进度如何推进| 社区征文
- Golang — template
- The solution of Chinese font garbled code in keil5
猜你喜欢

解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)

挡不住了,国产芯片再度突进,部分环节已进到4nm

【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】

Go language web development series 30: gin: grouping by version for routing

Unity EmbeddedBrowser浏览器插件事件通讯

Can newly graduated European college students get an offer from a major Internet company in the United States?

Qt学习17 对话框及其类型

Go language web development series 26: Gin framework: demonstrates the execution sequence of code when there are multiple middleware
![[技术发展-24]:现有物联网通信技术特点](/img/f3/a219fe8e7438b8974d2226b4c3d4a4.png)
[技术发展-24]:现有物联网通信技术特点

研发团队资源成本优化实践
随机推荐
双向链表(我们只需要关注插入和删除函数)
Error handling when adding files to SVN:.... \conf\svnserve conf:12: Option expected
Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
项目协作的进度如何推进| 社区征文
Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
从零开始的基于百度大脑EasyData的多人协同数据标注
Several common optimization methods matlab principle and depth analysis
Red hat satellite 6: better management of servers and clouds
MySQL data processing value addition, deletion and modification
软件测试工作那么难找,只有外包offer,我该去么?
CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
Go language unit test 3: go language uses gocovey library to do unit test
Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
Libuv库 - 设计概述(中文版)
windos 创建cordova 提示 因为在此系统上禁止运行脚本
JSON serialization case summary
Halcon combined with C # to detect surface defects -- Halcon routine autobahn
Dynamic programming 01 knapsack and complete knapsack
mysql中的字段问题
Universal dividend source code, supports the dividend of any B on the BSC