当前位置:网站首页>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;
}
边栏推荐
- Nacos configuration management
- Leetcode 322: Coin Change - 动态规划
- STL--stack容器
- Chengxin University envi_ IDL third week class content 1: reading OMI data (HDF5 file) and output + parsing
- 【Antv G2】折线图如何添加点击事件(点击任意位置即可获取折线上点的值)
- STL--queue容器
- 【工具篇】Unity2D人物控制器,控制2D玩家移动跳跃,四方向和水平方向
- 【Unity编辑器扩展】查找场景和资源内挂载某脚本的所有对象
- 【工具篇】Unity快速上手制作2D和2.5D游戏的神器TileMap
- 成信大ENVI_IDL第二周课后作业:提取n个点的气溶胶厚度+详细解析
猜你喜欢

VS Code 问题:launch:program‘...\.vscode\launch.exe‘ dose not exist

Gdb+vscode for debugging 0 - environment configuration

Envi IDL: lire la teneur en colonne de NO2 de tous les produits OMI et calculer la moyenne mensuelle, la moyenne trimestrielle, la moyenne annuelle + résolution

3D NFT的破茧重生:Caduceus去中心化边缘渲染技术

DQN理论基础及其代码实现【Pytorch + CartPole-v0】

第2章-系统控制原理 -> 经典控制理论

leetcode力扣经典问题——42.接雨水

STL--stack容器

Engineering compilation: makefile and cmake (I)

BUAAOS-Lab0实验报告
随机推荐
ENVI_ Idl: read the text file and output it in GeoTIFF format + simple mean interpolation
gdb+vscode进行调试7——程序出现segmentation default/段错误,如何进行调试?
【工具篇】Unity屏幕画线,Unity屏幕画图HSJ绘画工具
gdb+vscode进行调试5——gdb查看相关命令
Memory pooling of pooled components
动态规划问题 - 小兵向前冲
vscode+ros2环境配置
[tools] unity2d character controller, which controls 2D players to move and jump in four directions and horizontal directions
Build hue environment
毕业论文word技巧集合
[tools] unity screen drawing line, unity screen drawing Hsj drawing tool
Leetcode 198:House Robber
Vmware Tools最新安装教程(RHEL8)
Visual Studio 2019-QT调试
ENVI_IDL:读取OMI数据(HDF5)并输出为Geotiff文件+详细解析
Gdb+vscode for debugging 5 - GDB view relevant commands
Oozie integrated sh
池式组件之内存池篇
Set up sqoop environment
Swagger——世界上最流行的Api框架