概述
#include <stdio.h>
#include <deque>
#include <algorithm>
using namespace std;
class GreaterComp : public binary_function<const int&, const int&, bool>
{
public:
result_type operator()(first_argument_type& _Left, second_argument_type& _Right)
{
return _Left > _Right;
}
};
int main(int argc, char** argv)
{
//创建双向队列
deque<int> deq;
//往双向队列尾部加入元素,返回void
deq.push_back(1);
//往双向队列头部加入元素,返回void
deq.push_front(2);
//返回队列尾元素
printf("deq.back(): %dn", deq.back());
//返回队列头部元素
printf("deq.front(): %dn", deq.front());
//返回队列大小
printf("deq.size(): %dn", deq.size());
//弹出队列尾
deq.pop_back();
//弹出队列头
deq.pop_front();
printf("deq.size(): %dn", deq.size());
//往队列尾插入元素
deq.emplace_back(1);
//往队列头插入元素
deq.emplace_front(2);
printf("deq.front(): %d, deq.back(): %dn", deq.front(), deq.back());
//往队列指定位置插入元素,返回指向插入元素的迭代器
deque<int>::iterator iter = deq.emplace(deq.begin(), 3);
printf("*iter: %dn", *iter);
printf("deq.front(): %d, deq.back(): %dn", deq.front(), deq.back());
//清空队列
deq.clear();
printf("deq.size(): %dn", deq.size());
deq.push_back(100);
//在指定pos位置插入元素,返回指向插入元素的迭代器
iter = deq.insert(deq.begin(), 1);
printf("*iter: %dn", *iter);
printf("deq.front(): %d, deq.back(): %dn", deq.front(), deq.back());
printf("deq.size(): %dn", deq.size());
//在指定pos位置插入initializer_list,返回指向插入initializer_list第一个元素的迭代器
iter = deq.insert(deq.begin(), { 1, 2, 3 });
printf("*iter: %dn", *iter);
printf("deq.front(): %d, deq.back(): %dn", deq.front(), deq.back());
printf("deq.size(): %dn", deq.size());
deque<int> deqTemp;
deqTemp.push_back(1);
deqTemp.push_back(5);
//在指定pos位置插入[iter1, iter2)之间的元素,返回指向插入[iter1, iter2)区间第一个元素的迭代器
iter = deq.insert(deq.begin(), deqTemp.begin(), deqTemp.end());
printf("*iter: %dn", *iter);
printf("deq.front(): %d, deq.back(): %dn", deq.front(), deq.back());
printf("deq.size(): %dn", deq.size());
//删除指定位置的元素,返回下一个位置的元素的迭代器, 需要删除的元素必须存在,否则会出错
iter = deq.erase(deq.begin());
printf("*iter: %dn", *iter);
iter = deq.erase(deq.begin(), deq.end());
if (iter == deq.end())
printf("endn");
deq.push_back(1);
//返回pos位置的元素, 超出界限会引发out of range错误
printf("deq.at(%d): %dn", 0, deq.at(0));
printf("max_size(): %dn", deq.max_size());
//resize重新界定元素的数量,超出补0,多出删除
deq.resize(0);
printf("size: %dn", deq.size());
deq.resize(100);
printf("size: %d, back(): %dn", deq.size(), deq.back());
printf("empty(): %dn", deq.empty());
deq.clear();
//operator[] 下标操作符在序列式容器中使用必须容器了这个下标已经存在元素,如果这个下标尚未初始化会出错
//deq[0] = 1
出错
deq.push_back(2);
deq[0] = 10;
printf("back(): %dn", deq.back());
deq.push_back(1);
deque<int> jdeq;
jdeq.push_back(-100);
//给jdeq赋值
jdeq.assign(deq.begin(), deq.end());
printf("jdeq: ");
iter = jdeq.begin();
for (; iter != jdeq.end(); ++iter)
printf("%d ", *iter);
printf("n");
//排序
sort(jdeq.begin(), jdeq.end());
printf("jdeq: ");
iter = jdeq.begin();
for (; iter != jdeq.end(); ++iter)
printf("%d ", *iter);
printf("n");
deque<int> sort_deq;
for (int i = 0; i < 10; i++)
sort_deq.push_back(i);
sort(sort_deq.begin(), sort_deq.end(), GreaterComp());
iter = sort_deq.begin();
for (; iter != sort_deq.end(); ++iter)
printf("%d ", *iter);
printf("n");
//初始化
//deque<int> deq1(10);
//deque<int> deq2(10, 1);
//deque<int> deq3(deq1.begin(), deq1.end());
//deque<int> deq4({ 1, 2, 3 });
getchar();
return 0;
}
运行结果为:
deq.back(): 1
deq.front(): 2
deq.size(): 2
deq.size(): 0
deq.front(): 2, deq.back(): 1
*iter: 3
deq.front(): 3, deq.back(): 1
deq.size(): 0
*iter: 1
deq.front(): 1, deq.back(): 100
deq.size(): 2
*iter: 1
deq.front(): 1, deq.back(): 100
deq.size(): 5
*iter: 1
deq.front(): 1, deq.back(): 100
deq.size(): 7
*iter: 5
end
deq.at(0): 1
max_size(): 1073741823
size: 0
size: 100, back(): 0
empty(): 0
back(): 10
jdeq: 10 1
jdeq: 1 10
9 8 7 6 5 4 3 2 1 0
最后
以上就是无限金毛为你收集整理的deque的用法的全部内容,希望文章能够帮你解决deque的用法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复