C++ STL 中的 deque_clear() 和 deque_erase()
本任务旨在演示 C++ STL 中 deque clear() 和 deque erase() 函数的功能。
什么是 Deque
Deque 是双端队列,是一种序列容器,可以在两端进行扩展和收缩。队列数据结构只允许用户在末尾插入数据,在头部删除数据。让我们以公交车站的队列为例,乘客只能从队列的末尾加入,而站在头部的人最先离开;而在双端队列中,可以在两端进行数据插入和删除。
什么是 deque.clear()
此函数用于移除 deque 中的所有元素,从而使其大小变为 0。
语法
dequename.clear( )
dequename.clear()
输入 Deque − 96 97 98 100
输出 Deque − 空
输入 Deque − 1 2 3 4 5 6
输出 Deque − 空
可采用的方法
首先,我们声明 deque。
然后,我们打印 deque。
然后,我们定义 clear() 函数。
使用上述方法,我们可以清除所有 deque。
示例
// C++ code to demonstrate the working of deque.clear( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
// initializing the deque
Deque<int> deque = { 85, 87, 88, 89, 90 };
// print the deque
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
// defining clear( ) function
deque.clear( );
// printing new deque
cout<< “ New Deque:”;
for( x = deque.begin( ) ; x != deque.end( ); ++x)
cout<< “ “ <<*x;
return 0;
}输出
运行以上代码将生成以下输出
Input - Deque: 85 87 88 89 90 Output - New Deque: No Output
广告
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP