当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Qt学习20 Qt 中的标准对话框(中)

Brief analysis of tensorboard visual processing cases

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

太阳底下无新事,元宇宙能否更上层楼?

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

Go language unit test 4: go language uses gomonkey to test functions or methods

MySQL data processing value addition, deletion and modification

Unity embeddedbrowser browser plug-in event communication

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

Libuv库 - 设计概述(中文版)
随机推荐
Golang — template
CVPR 2022 | 美团技术团队精选6篇优秀论文解读
Golang - command line tool Cobra
Qt学习23 布局管理器(二)
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Replace the GPU card number when pytorch loads the historical model, map_ Location settings
MapReduce implements matrix multiplication - implementation code
Go 1.16.4: purpose of go mod tidy
Internet of things completion -- (stm32f407 connects to cloud platform detection data)
SQL Injection (GET/Search)
Field problems in MySQL
Conversion function and explicit
Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)
[sort] bucket sort
Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm
JVM系列——概述,程序计数器day1-1
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
Unity render streaming communicates with unity through JS
Go language web development series 27: Gin framework: using gin swagger to implement interface documents
Depth and breadth first traversal of tree (regardless of binary tree)