当前位置:网站首页>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 .
边栏推荐
- 研发团队资源成本优化实践
- Universal dividend source code, supports the dividend of any B on the BSC
- Go language unit test 4: go language uses gomonkey to test functions or methods
- 【556. 下一个更大元素 III】
- Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:
- Leetcode-1175.Prime Arrangements
- 【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
- Screenshot of the operation steps of upload labs level 4-level 9
- Which securities company has the lowest Commission for opening an account online? I want to open an account. Is it safe for the online account manager to open an account
- 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
猜你喜欢
解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)
GoLand 2021.2 configure go (go1.17.6)
GoLand 2021.1: rename the go project
Setting up remote links to MySQL on Linux
【BW16 应用篇】安信可BW16模组与开发板更新固件烧录说明
Golang - command line tool Cobra
Internet of things completion -- (stm32f407 connects to cloud platform detection data)
从零开始的基于百度大脑EasyData的多人协同数据标注
使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
Go language web development series 30: gin: grouping by version for routing
随机推荐
3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)
SQL Injection (POST/Search)
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
[技術發展-24]:現有物聯網通信技術特點
使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
The network card fails to start after the cold migration of the server hard disk
[redis] cache warm-up, cache avalanche and cache breakdown
Qt学习23 布局管理器(二)
Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu
Multi person collaborative data annotation based on Baidu brain easydata from scratch
Heap structure and heap sort heapify
How to use lxml to judge whether the website announcement is updated
IBEM 数学公式检测数据集
Libuv Library - Design Overview (Chinese version)
Screenshot of the operation steps of upload labs level 4-level 9
Unity Render Streaming通过Js与Unity自定义通讯
Comprehensively develop the main channel of digital economy and digital group, and actively promote the utonmos digital Tibet market
Flutter动态化 | Fair 2.5.0 新版本特性
Can newly graduated European college students get an offer from a major Internet company in the United States?