首页 文章资讯内容详情

C ++中STL的deque_clear和deque_erase方法

2026-06-04 1 花语

给出的任务是显示C++STL中的双端队列clear()和双端队列delete()函数的功能。

什么是双端队列

双端队列是双端队列,它是序列容器,在两端都提供扩展和收缩功能。队列数据结构允许用户仅在END插入数据,并从FRONT删除数据。让我们以在公交车站排队的类比为例,那里的人只能从END插入队列,而站在FRONT的人是第一个被移走的人,而在双头队列中,可以在这两个地方插入和删除数据结束。

什么是deque.clear()

此函数用于删除双端队列的所有元素,从而使其大小为0。

语法

dequename.clear( )

dequename.clear()

输入双端队列-969798100

输出双端队列-空

输入双端队列&mijnus;123456

输出双端队列-空

可以遵循的方法

首先我们声明双端队列。

然后我们打印双端队列。

然后,我们定义clear()函数。

通过使用上述方法,我们可以清除所有双端队列。

示例

// 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