无涯教程-C++ Deque - clear()函数

62 阅读1分钟

C++ Deque clear()函数从双端队列中删除所有元素,并且双端队列的大小减小为零。

clear - 语法

void clear();

clear - 返回值

它不返回任何值。

clear - 例子

让我们看一个简单的例子

#include <iostream>
#include<deque>
using namespace std;
int main()
{
    deque<int> first;
    deque<int>::iterator itr;
    cout<<"Content of first deque:";
    first.push_back(1);
    first.push_back(2);
    first.push_back(3);
    for(itr=first.begin();itr!=first.end();++itr)
    cout<<*itr<<" ";
    cout<<
;
    first.clear();
    cout<<"Now,Content of first deque:";
    first.push_back(4);
    first.push_back(5);
    first.push_back(6);
    for(itr=first.begin();itr!=first.end();++itr)
    cout<<*itr<<" ";
    return 0;
}

输出:

Content of first deque:1 2 3 
Now,Content of first deque:4 5 6 

参考链接

www.learnfk.com/c++/cpp-deq…