当前位置:网站首页>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
- Swiftui development experience: the five most powerful principles that a programmer needs to master
- GoLand 2021.2 configure go (go1.17.6)
- Can newly graduated European college students get an offer from a major Internet company in the United States?
- 使用vscode查看Hex或UTF-8编码
- Flutter动态化 | Fair 2.5.0 新版本特性
- Bidirectional linked list (we only need to pay attention to insert and delete functions)
- SQL Injection (POST/Search)
- The shadow of the object at the edge of the untiy world flickers, and the shadow of the object near the far point is normal
- RichView TRVStyle ListStyle 列表样式(项目符号编号)
猜你喜欢

Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)

Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)

Go language unit test 3: go language uses gocovey library to do unit test

双向链表(我们只需要关注插入和删除函数)

Common network state detection and analysis tools

【BW16 应用篇】安信可BW16模组与开发板更新固件烧录说明

Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock

Resource Cost Optimization Practice of R & D team
![[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update](/img/b8/31609303fd817c48b6fff7c43f31e5.png)
[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update

Qt学习22 布局管理器(一)
随机推荐
Dynamic programming 01 knapsack and complete knapsack
PhpMyAdmin stage file contains analysis traceability
Sequence table (implemented in C language)
[understanding by chance-37]: the structure of human sensory system determines that human beings are self-centered
3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
GoLand 2021.1: rename the go project
Software testing is so hard to find, only outsourcing offers, should I go?
Using registered classes to realize specific type matching function template
Unity EmbeddedBrowser浏览器插件事件通讯
项目协作的进度如何推进| 社区征文
Thrift threadmanager and three monitors
Static linked list (subscript of array instead of pointer)
研发团队资源成本优化实践
Record 405 questions about bank callback post request
Can newly graduated European college students get an offer from a major Internet company in the United States?
8 Queen question
Logback log sorting
Qt学习20 Qt 中的标准对话框(中)
掌握Cypress命令行选项,是真正掌握Cypress的基础