当前位置:网站首页>Most detailed collation of vector basis of STL
Most detailed collation of vector basis of STL
2022-06-21 20:48:00 【Radish says vegetables】
Preface :
Vector Is a kind of dynamic array that can store any type , Is a sequential container , It can be used sort Sort it , The underlying data structure is an array , Elements can be accessed randomly .Vectors Contains a series of continuously stored elements , It behaves like an array . visit Vector Any element in or adding an element from the end can be done within constant level time complexity , The location of the element looking for a specific value or Vector Inserting elements in is linear time complexity .
notes : If you want to use vector, You need to add vector The header file , namely #include<vector>, besides , You also need to add using namespace std;
vector The definition of
C++ Middle structure vector You can use any of the following parameter methods :
grammar :
1.vector();
2.vector(size_type num, const TYPE &val);
3.vector(const vector &from);
4.vector(input_iterator start, input_iterator end);
1· No parameter - Construct an empty vector
2· Number (num) And the value (val) - Construct an initial num The values are val The elements of the Vector
3·vector(from) - Construct a relationship with vector from same vector
4· iterator (start) And iterators (end) - Construct an initial value of [start,end) Interval element Vector( notes : Semi open interval )
give an example , Suppose you construct a containing 3 The values are 4 Of vector
vector<int> v1(3,4);
visit vector Methods for elements in containers
1. Access by subscript
It's the same as accessing an ordinary array , For a defined as vector<typename> v Of vector Containers , Use it directly v[index] Just visit , among index∈[0,v.size()−1]
2. Access via iterator
An iterator can be understood as something like a pointer
vector<typename>::iterator it; Define iterator , Got the iterator it after , Can pass *it To visit vector The elements in
3.v[i] and *(v.begin()+i) It is equivalent.
4. Iterator to implement two kinds of self addition and self subtraction operations
5.begin() The action of a function is to take v First element address of ,end() Function is not meant to take v Tail element address of , But take the next address of the tail element address . It serves as the iterator end flag , Do not store any elements
6. In common use STL In the container , Only in vector and string in , Only allowed to use v.begin()+3 This iterator is written with an integer
vector Common functions
vector Time complexity of common functions :
push_back(), The time complexity is O(1)
pop_back(), The time complexity is O(1)
size(), The time complexity is O(1)
clear(), The time complexity is O(N)
insert(), The time complexity is O(N)
erase(), The time complexity is O(N)
vector Interpretation of common functions :
push_back(x) Is in the vector Add an element after x
pop_back() Used to delete vector The tail element
size() Used to obtain vector The number of elements in , The return is unsigned type , But generally speaking %d It won't be a big problem , This is true for all STL The containers are the same
clear() For emptying vector All elements in
insert(it,x) Used to direct to vector Any iterator of it Insert an element at x
erase():
Delete single element ,erase(it) That is, delete the iterator as it The element of .
Delete all elements in an interval ,erase(first,last) Delete immediately [first,last) All the elements inside
Empty vector You can also use v.erase(v.begin(),v.end());
Other related functions :

Some function usage examples :
Here is a vector Sample code , The contents are as follows :
#include <vector>
#include <iostream>
using namespace std;
int main()
{
typedef vector<int> vecInt;
vecInt vecint;
// Add data
for (int i = 0; i < 5; i++)
{
vecint.push_back(i+1);
}
// Traversing elements
vecInt::iterator it;
cout << "vecint is: " << endl;
for (it = vecint.begin(); it != vecint.end(); it++)
{
cout << *it << endl;
}
// Query data
cout << "the second element is: " << vecint.at(1) << endl;
// Delete data , Delete the last element
it = vecint.end();
it = it - 1; // We need to pay attention to ,(it - 1) Corresponds to the last element
vecint.erase(it);
// Traverse the elements again , Observe whether the deletion operation is successful
cout << "after del, vecint is: " << endl;
for (it = vecint.begin(); it != vecint.end(); it++)
{
cout << *it << endl;
}
// insert data , Insert data before the last element
it = vecint.end();
it = it - 1;
vecint.insert(it, 100);
// Traverse the elements again , Observe whether the insertion operation is successful
cout << "after insert, vecint is: " << endl;
for (it = vecint.begin(); it != vecint.end(); it++)
{
cout << *it << endl;
}
return 0;
}
The result of the above code is as follows :

vector Advanced usage of
Dynamic arrays can store more than just basic data types , It can also store custom data types , For example, structure .
struct Student {
string name; // name
int age; // Age
};
int main() {
vector<Student> class1; // class
Student stu1, stu2;// Student 1, Student 2
stu1.name = "luobo";
stu1.age = 12;
stu2.name = "xiaoming";
stu2.age = 25;
class1.push_back(stu1);
class1.push_back(stu2);
return 0;
}
It can be used in the following ways vector Define a two-dimensional array :
vector<vector<int>> vecvec = { {1, 2, 8}, {2, 4, 9}, {4, 7, 10}, {6, 8, 11}};
Actually , We can quickly build such a dynamic array through a constructor , Using constructor is equivalent to using loop , By using constructors appropriately , Can reduce the amount of code .
边栏推荐
- LN2220 2A过流5V1A高效率升压IC芯片 DC/DC 电压调整器
- 国标设备注册EasyCVR平台,如何修改设备在离线状态判断的时间?
- Flutter 输入框组件
- Servlet usage
- JMeter thread duration
- What is more advantageous than domestic spot silver?
- TX9116同步升压ic
- 决策树(Decision Tree)学习笔记
- pfSense配置TINC站點至站點隧道教程
- Visualization of operation and maintenance monitoring data - let the data speak [Huahui data]
猜你喜欢

高度可扩展,EMQX 5.0 达成 1 亿 MQTT 连接

Harbor high availability cluster design and deployment (practice + video), based on offline installation

JVM memory structure

用户态热补丁原理与应用

4.3寸触摸屏智能网络中控主机有哪些应用

Alibaba cloud ack one and ACK cloud native AI suite have been newly released to meet the needs of the end of the computing era

How does the easycvr intelligent edge gateway hardware set power on self start?

What noteworthy technologies of gold: the importance of fund management

Redis HyperLogLog 是什么?这些场景使用让我枪出如龙一笑破苍穹

Flink CDC MongoDB Connector 的实现原理和使用实践
随机推荐
【基于合泰HT32F52352的智慧垃圾桶总结】
EasyCVR智能边缘网关硬件如何设置通电自启动?
STL之vector扩容机制
Netcore3.1 Ping whether the network is unblocked and obtaining the CPU and memory utilization of the server
DO280OpenShift命令及故障排查--访问资源和资源类型
Which platform is the best and safest for retail investors to buy funds
阿里云 ACK One、ACK 云原生 AI 套件新发布,解决算力时代下场景化需求
BTC投资者损失预计达73亿美元!“割肉式”抛售来袭?加密寒冬比预期更冷、更长!
高度可扩展,EMQX 5.0 达成 1 亿 MQTT 连接
IAR重大升级,支持VS Code,ST发布第一个带有处理单元的传感器
Inno setup window drag learning
pfSense配置TINC站点至站点隧道教程
I remember that procedure cannot be written in maxcomputer. If you want to use Oracle procedure
Analysis of ${} string splicing in JS
Anfulai embedded weekly report (issue 270): June 13, 2022 to June 19, 2022
随机森林(Random Forest)学习笔记
Flutter 输入框组件
The difference between break and continue
数组的最小不可组成和问题
服务器正文17:内存映射和共享内存的简单理解