当前位置:网站首页>STL -- deque container
STL -- deque container
2022-07-19 02:19:00 【hjl011006】
One 、deque Basic concept of container
- function : Double ended array , You can insert and delete the header
- And vector The difference between :
- vector For the insertion and deletion of the head, the efficiency is low , More data , The less efficient
- deque Relatively speaking , The insertion and deletion of the head is faster than vector fast
- vector Accessing elements will be faster than deque fast , This has to do with the internal implementation of both
- picture :

- working principle :

- characteristic :vector Container iterators are iterators that support random access ( Skip access ).
Two 、deque Constructors
- deque<T> deqT; // Default structure
- deque(beg, end); // The constructor will (beg,end) The elements in the interval are copied to themselves
- deque(n, elem); // The constructor will n individual elem Copy to yourself
- deque(const deque & deq); // copy constructor
#include<iostream>
#include<deque>
using namespace std;
void print_Deque(deque<int>&d)
{
for(deque<int>::const_iterator it = d.begin();it!=d.end();it++) // Set the function to read only
{
cout<<*it<<" ";
}
cout<<endl;
}
int main()
// Default structure , No arguments structure
deque<int> d1;
for(int i=0;i<10;i++)
{
d1.push_back(i);
}
print_Deque(d1);
// Interval assignment
deque<int>d2(d1.begin(),d1.end());
print_Deque(d2);
// n individual elem
deque<int> d3(10,100);
print_Deque(d3);
// Copy structure
deque<int> d4(d3);
print_Deque(d4);
return 0;
}
3、 ... and 、deque Assignment operation
- deque & operator=(const deque & deq); // Overload the equal sign operator
- assign(beg, end); // take (beg,end) The copy of the data in the interval is assigned to itself
- assign(n, elem); // take n individual elem Copy assignment to itself
#include<iostream>
#include<deque>
using namespace std;
void print_Deque(deque<int>&d)
{
for(deque<int>::const_iterator it = d.begin();it!=d.end();it++) // Set the function to read only
{
cout<<*it<<" ";
}
cout<<endl;
}
int main()
{
deque<int> d1;
for(int i=0;i<10;i++)
{
d1.push_back(i);
}
print_Deque(d1);
// Equal sign assignment
deque<int> d2;
d2 = d1;
print_Deque(d2);
// assign Interval assignment
deque<int> d3;
d3.assign(d1.begin(),d1.end());
print_Deque(d3);
// assign n individual elem assignment
deque<int> d4;
d4.assign(10,100);
print_Deque(d4);
return 0;
}
Four 、deque Size operation
- deque.empty(); // Judge whether the container is empty
- deque.size(); // Returns the number of elements in the container
- deque.resize(); // Reassign the container length to num, If the container becomes longer , Then fill with the default value of zero
// If the container gets shorter , Then the element beyond the length of the container at the end is deleted
- deque.resize(num ,elem); // Reassign the container length to num, If the container becomes longer , with elem Value to fill the new position
// If the container gets shorter , Then the element beyond the length of the container at the end is deleted
#include<iostream>
#include<deque>
using namespace std;
void print_Deque(deque<int>&d)
{
for(deque<int>::const_iterator it = d.begin();it!=d.end();it++) // Set the function to read only
{
cout<<*it<<" ";
}
cout<<endl;
}
int main()
{
deque<int> d1;
for(int i=0;i<10;i++)
{
d1.push_back(i);
}
print_Deque(d1);
cout<<" Judge whether the container is empty :( Empty return 1, Not empty return 0) "<<d1.empty()<<endl;
cout<<"deque Size of container :"<<d1.size()<<endl;
// Reset size
d1.resize(20);
print_Deque(d1);// The default is 0 Fill in excess positions
d1.resize(6); // The excess part is deleted
print_Deque(d1);
// Overload version
d1.resize(20,5);// The empty part is used 5 fill
print_Deque(d1);
return 0;
}
5、 ... and 、deque Insert and delete
1. Two end insertion operation :
- push_back(elem); // Add a data at the end of the container
- push_front(elem); // Insert a data in the head of the container
- pop_back(); // Delete the last data in the container
- pop_front(); // Delete the first data of the container
2. Specified location operation :
- insert(pos, elem); // stay pos Insert a elem Copies of elements , Returns the location of the new element
- insert(pos, n, elem); // stay pos Position insert n individual elem data , No return value
- insert(pos, beg, end); // stay pos Position insert (beg,end) Interval data , No return value
- erase(beg, end); // Delete (beg,end) Data in the interval , Return to the next data location
- erase(pos); // Delete pos Location data , Return to the next data location
- clear(); // Empty all data in the container
#include<iostream>
#include<deque>
using namespace std;
void print_Deque(deque<int>&d)
{
for(deque<int>::const_iterator it = d.begin();it!=d.end();it++) // Set the function to read only
{
cout<<*it<<" ";
}
cout<<endl;
}
int main()
{
deque<int> d1;
for(int i=0;i<10;i++)
{
d1.push_back(i);
}
print_Deque(d1);
// Head insertion , Tail insertion
d1.push_back(10);
d1.push_front(20);
print_Deque(d1);
d1.pop_back();
d1.pop_front();
print_Deque(d1);
// Insert
d1.insert(d1.begin(),1000);// The first parameter is the iterator
print_Deque(d1);
// Overload version
d1.insert(d1.begin(),2,2000);// Insert n individual elem
print_Deque(d1);
// Insert interval
deque<int> d2;
for(int i=0;i<10;i++)
{
d2.push_back(10+i);
}
print_Deque(d2);
d1.insert(d1.end(),d2.begin(),d2.end());
print_Deque(d1);
// Delete
d1.erase(d1.begin());
print_Deque(d1);
// Delete by section
d1.erase(d2.begin(),d2.end());
print_Deque(d1);
// Empty
d1.clear();
print_Deque(d1);
return 0;
}
6、 ... and 、deque Data access
- at(int idx); // Returns the index idx The data
- operator[]; // Returns the index idx The data
- front(); // Returns the first data in the container
- back(); // Returns the last element of the container
#include<iostream>
#include<deque>
using namespace std;
int main()
{
deque<int> d1;
for(int i=0;i<10;i++)
{
d1.push_back(i);
}
for(int i=0;i<d1.size();i++)
{
cout<<d1[i]<<" ";
}
cout<<endl;
for(int i=0;i<d1.size();i++)
{
cout<<d1.at(i)<<" ";
}
cout<<endl;
// Return header and footer data
cout<<" The first data :"<<d1.front()<<endl;
cout<<" The last data :"<<d1.back()<<endl;
return 0;
}
7、 ... and 、deque Sort
- sort(iterator beg ,iterator end); // Yes beg and end Sort the elements in the interval
For containers that support random access iterators , All available sort The algorithm sorts them directly
vector Containers can also be used sort Sort
#include<iostream>
#include<deque>
#include<algorithm>
using namespace std;
void print_Deque(const deque<int>&d)
{
for(deque<int>::const_iterator it = d.begin();it!=d.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
int main()
{
deque<int> d1;
d1.push_back(2);
d1.push_back(7);
d1.push_back(9);
d1.push_back(2);
d1.push_back(5);
print_Deque(d1);
sort(d1.begin(),d1.end()); // The default is ascending
print_Deque(d1);
return 0;
}
边栏推荐
- ENVI_IDL:批量重投影ModisSwath产品(调用二次开发接口)+解析
- 3D NFT的破茧重生:Caduceus去中心化边缘渲染技术
- [tools] unity2d character controller, which controls 2D players to move and jump in four directions and horizontal directions
- 【动态规划百题强化计划】1~10(持续更新中)
- 浏览器无法打开Tensorboard
- 第二讲 BTC-密码学原理(笔记)
- Labelme 的简单用法和界面介绍
- 【Unity编辑器扩展】Unity制作自己的专属的编辑器面板
- Double Q-Learning理论基础及其代码实现【Pendulum-v0】
- [unity development tips] unity packs the EXE on the PC side and compresses and packs it into an EXE file
猜你喜欢
![[unity Editor Extension] the pre-processing and post-processing pictures of unity assets are automatically transferred to sprite2d](/img/84/34879d5c0b6e19d599c99eec944c26.png)
[unity Editor Extension] the pre-processing and post-processing pictures of unity assets are automatically transferred to sprite2d

池式组件之内存池篇

Labelme 的简单用法和界面介绍

Installing MySQL and JDBC on Windows

ENVI_ IDL: read OMI data (HDF5) and output it as GeoTIFF file + detailed parsing

Hash table, bloom filter, distributed consistency hash

Buaaos-lab0 experimental report

ENVI_ Idl: batch re projection of modisswath products (calling the secondary development interface) + parsing

Double Q-Learning理论基础及其代码实现【Pendulum-v0】

【解决方案】win11中本地组策略编辑器(gpedit.msc)打不开
随机推荐
Memory pooling of pooled components
STL -- string container
【Unity编辑器扩展】Unity内部Asset资源配置ScriptableObject
ENVI_ Idl: reading of text files (mainly txt and CSV files)
gdb+vscode进行调试4——gdb执行相关命令
[tools] unity screen drawing line, unity screen drawing Hsj drawing tool
gdb+vscode进行调试5——gdb查看相关命令
gdb+vscode进行调试1——使用CMakelist文件进行编译和调试+附加进程调试
ENVI_IDL: 读取文本文件并输出为Geotiff格式+简单均值插值
《Visual C#从入门到精通》个人学习整理
gdb+vscode进行调试6——gdb调试多线程命令札记
图像质量评估指标:SNR、PSNR、MSE和SSIM
Hash table, bloom filter, distributed consistency hash
[hdrp HD rendering pipeline] create hdrp project and upgrade the built-in pipeline project to hdrp project
Leetcode 70:Climbing Stairs
[unity panel attribute literacy] set texture import settings after importing textures
【HDRP高清渲染管道】创建HDRP工程,把内置管线工程升级为HDRP工程
BladeX——精心设计的微服务架构
ENVI_ Idl: batch Reproject MODIS swath products and specify the range output as GeoTIFF format + parsing
【Unity编辑器扩展】Unity发布资源到商店流程Unity Asset Store Publish