当前位置:网站首页>Usage of vector common interface
Usage of vector common interface
2022-04-23 02:09:00 【Xi Feng】
Catalog
repair : An iterator is a left closed right open interval
vector
Simple understanding vector Is a sequence container that represents a variable size array
vector Use
One 、vector The definition of
| Constructor declaration | Interface specification | Sequence |
|---|---|---|
| vector() | Nonparametric construction | 1 |
| vector(size_type n,const value_type& val=value_type()) | Construct and initialize n individual val | 2 |
| vector(const vector& x) | Copy structure | 3 |
| vector (InputIterator first, InputIterator last); | Use iterators for initial session construction | 4 |
//1
vector<int> arr1;
//3
vector<int> arr3(arr2);
//2
vector<int> arr4(5,0);
//4 This is implemented using iterators
string std("hello");
vector<char> ch1(std.begin(), std.end());
//5 It can also be initialized like this, but C++98 This is not supported
vector<int> arr2 = { 1,2,3,4,5 };
Two 、vector iterator Use
| iterator Use | Interface specification |
|---|---|
| begin()+end() | Get the first data location of iterator/const_iterator, Get the next location of the last data Of iterator/const_iterator |
| rbegin()+rend() | Get the last data location of reverse_iterator, Get the first data of the previous position reverse_iterator |
// Then, as defined above ch1 and arr3
// If you don't want to write so long, you can also use auto Automatic derivation
//for (auto it = ch1.begin(); it != ch1.end(); ++it)
for (vector<char>::iterator it = ch1.begin(); it != ch1.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
//for (auto it = arr3.rbegin(); it != arr3.rend(); ++it)
for (vector<int>::reverse_iterator it = arr3.rbegin(); it != arr3.rend(); ++it)
{
cout << *it << " ";
}
cout << endl;
3、 ... and 、vector Spatial growth
| Capacity space | Interface specification |
|---|---|
| size | Get the number of data |
| capacity | Get capacity size |
| empty | Determine whether it is null |
| resize | change vector Of size |
| reserve | change vector Put in capacity |


-
capacity Code in vs and g++ The growth of is different ,vs Next capacity Is in accordance with the 1.5 Multiplied by ,g++ Is in accordance with the 2 Multiplied by (vs yes PJ Version of STL,g++ yes SGI Version of STL)
-
reserve Only responsible for opening up space , If you're sure you know how much space you need ,reserve Can ease vector The cost of capacity expansion is flawed
-
resize In the open space at the same time also will carry on the initialization , It will affect size
-
resize If appear size Smaller than the original situation , Although it won't reduce the space, it will change size

Four 、vector Add or delete check change
| vector Add or delete check change | Interface specification |
|---|---|
| push_back | Tail insertion |
| pop_back | Deletion at the end |
| find | lookup ( This is implemented by the algorithm module , No vector Member interface of ) |
| insert | stay position Insert before val |
| erase | Delete posistion Location data |
| swap | Two exchanges vector Data space of |
| operator[] | Access like an array |
| clear | Empty data , Will not clear the space |
| assign | The new content is from first and Last An element constructed between each element in the range , Same order ; The new content is n individual Elements , Each element is initialized to val Copy of . If reallocation occurs , Then use Internal distributor distribution Required storage . |
vector<int>arr1;
// Tail insert a data
arr1.push_back(1);
arr1.push_back(2);
arr1.push_back(3);
arr1.push_back(4);
arr1.push_back(5);
// Use iterators to traverse
for (vector<int>::iterator it = arr1.begin(); it != arr1.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
// On the left is the iterator , On the right is the data to be inserted
arr1.insert(arr1.begin() + 3, 6);
arr1.insert(arr1.begin() + 3, 7);
for (vector<int>::iterator it = arr1.begin(); it != arr1.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
// Delete a data at the end , Call directly without parameters
arr1.pop_back();
arr1.pop_back();
for (vector<int>::iterator it = arr1.begin(); it != arr1.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
// The iterator passes the position
arr1.erase(arr1.end()-2);
for (vector<int>::iterator it = arr1.begin(); it != arr1.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
vector<int> arr2;
// In exchange for
swap(arr1, arr2);
swap(arr2[0], arr2[3]);
for (vector<int>::iterator it = arr1.begin(); it != arr1.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
for (vector<int>::iterator it = arr2.begin(); it != arr2.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
arr1 = { 9,8,7,6,5 };
for (vector<int>::iterator it = arr1.begin(); it != arr1.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
// Empty data
arr1.clear();
for (vector<int>::iterator it = arr1.begin(); it != arr1.end(); ++it)
{
cout << *it << " ";
}
cout << endl;

vector<int>arr1 = { 9,8,7,6,5 };
vector<int>arr2 = { 0,1,2,3,4,10,11,12 };
// By iterator , You can give arr1 Assign data to other objects
// If the assigned object is larger than the original object , It will be automatically expanded to... With large objects capacity identical
arr1.assign(arr2.begin(), arr2.end());
for (vector<int>::iterator it = arr1.begin(); it != arr1.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
// On the left is a number , On the right is the value , It's just Fu 8 individual 0
arr1.assign(8, 0);
for (vector<int>::iterator it = arr1.begin(); it != arr1.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
//find No vector Member interface of , It's in the algorithm library , Must include header file when using #include<algorithm>
vector<int>arr1 = {
1,2,3,4,5,6,7,8 };
//find The first two parameters pass an iterator interval , The third parameter is the data to be searched
// If it can be found, it returns the iterator that finds the data location , If you can't find it, go back end(), So judge
vector<int>::iterator it=find(arr1.begin(), arr1.end(),9);
if (it != arr1.end())
{
cout << *it << endl;
}
版权声明
本文为[Xi Feng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220836487792.html
边栏推荐
- Shardingsphere broadcast table and binding table
- 006_redis_SortedSet类型
- Tp6 Alibaba cloud SMS window reports curl error 60: SSL certificate problem: unable to get local issuer certificate
- Some tips for using proxy IP.
- 代理IP可用率是不是等同于代理IP的效率?
- 小程序 canvas 画布半圆环
- MySQL C language connection
- Chinese scientists reveal a new mechanism for breaking through the bottleneck of rice yield
- What is BGP server and what are its advantages?
- Is CICC fortune a company with CICC? Is it safe
猜你喜欢

Dynamic memory management

Esp32 message queue using FreeRTOS

什么是api接口?
![[hands on learning] network depth v2.1 Sequence model](/img/51/0de4c7972a99151007a8f27f351c83.png)
[hands on learning] network depth v2.1 Sequence model

有哪些常见的代理ip问题?

Campus transfer second-hand market source code

easyswoole环境配置
![[assembly language] understand](/img/73/2483bca93714e378ff5eef18bddcd1.jpg)
[assembly language] understand "stack" from the lowest point of view

动态代理ip的测试步骤有哪些?

What business scenarios will the BGP server be used in?
随机推荐
How to write the resume of Software Test Engineer so that HR can see it?
They are all intelligent in the whole house. What's the difference between aqara and homekit?
php 2022年4月20面试题整理
今天终于会写System.out.println()了
Quel est le fichier makefile?
013_基于Session实现短信验证码登录流程分析
How to call out services in idea and display the startup class in services
【Chrome扩展程序】content_script的跨域问题
Halo open source project learning (I): project launch
How does Axure set the content of the text box to the current date when the page is loaded
Use of push() and pop()
What should I pay attention to when using proxy IP?
tp6阿里云短信 window 报 cURL error 60: SSL certificate problem: unable to get local issuer certificate
Is the sinking coffee industry a false prosperity or the eve of a broken situation?
从开源爱好者到 Apache 董事,一共分几步?
Applet reads files
Some tips for using proxy IP.
Is CICC fortune a state-owned enterprise and is it safe to open an account
Log4j2 configuration
Shardingsphere broadcast table and binding table