//查找
const int FINDNUMBER = 19;
printf("\n查找%d\n", FINDNUMBER);
pos = find(ideq.begin(), ideq.end(), FINDNUMBER);
if (pos != ideq.end())
printf("find %d success\n", *pos);
else
printf("find failed\n");
//在头尾删除数据
printf("\n在头尾删除数据...\n");
ideq.pop_back();
ideq.pop_front();
//在头尾加入新数据
printf("\n在头尾加入新数据...\n");
ideq.push_back(100);
ideq.push_front(i);
//operator=赋值运算符重载
deque<int> d1 {1,2,3,4,5},d2;
d2=d1;
deque<int>::iterator it;
for(it=d2.begin();it!=d2.end();it++)
cout << *it << " ";
cout << endl;
//c.front()返回c容器的第一个元素
//c.back()返回c容器的最后一个元素
deque<int> d {1,2,3,4,5};
if(!d.empty())
{
cout << "d.front():" << d.front() << endl;
cout << "d.back(): " << d.back() << endl;
}
//c.size()返回c容器中实际拥有的元素个数
deque<int> d {1,2,3,4,5};
cout << "d.size():" << d.size() << endl;
//c.assign(n,num)将n个num拷贝复制到容器c
deque<int> d1 {1,2,3,4,5},d2;
d2.assign(2,8);
deque<int>::iterator it;
cout << "d2.assign(n,num):";
for(it=d2.begin();it!=d2.end();it++)
cout << *it << " ";
//c.assign(beg,end)将[beg,end)区间的数据拷贝复制到容器c
d2.assign(d1.begin(), d1.begin()+3);
cout << "d2.assign(beg,end):";
for(it=d2.begin();it!=d2.end();it++)
cout << *it << " ";
cout << endl;
//c.at(pos)返回索引为pos的位置的元素,会执行边界检查,如果越界抛出out_of_range异常
deque<int> d{1,2,3,4,5};//VS2012以下编译器不支持这种写法
cout <<"d.at(pos):"<<d.at(2);
//c.at(pos)返回索引为pos的位置的元素,会执行边界检查,如果越界抛出out_of_range异常
deque<int> d {1,2,3,4,5};
cout << "d[2]:" << d[2];
//c.empty()判断c容器是否为空
deque<int> d {1,2,3,4,5};
if(!d.empty())
cout << "d is not empty!" << endl;
else
cout << "d is empty!" << endl;
//c.max_size()返回c容器可能存放元素的最大数量
deque<int> d {1,2,3,4,5};
cout << "d.max_size():" << d.max_size() << endl;
//c.clear()清除c容器中拥有的所有元素
deque<int> d {1,2,3,4,5};
deque<int>::iterator it;
cout << "clear before:" ;
for(it=d.begin();it!=d.end();it++)
cout << *it << " ";
cout << endl;
d.clear();
cout << "clear after:" ;
for(it=d.begin();it!=d.end();it++)
cout << *it << " ";
cout << endl;
//c.insert(pos,num)在pos位置插入元素num
//c.insert(pos,n,num)在pos位置插入n个元素num
//c.insert(pos,beg,end)在pos位置插入区间为[beg,end)的元素
deque<int> d {1,2,3,4,5};
deque<int>::iterator it;
cout << "insert before:" ;
for(it=d.begin();it!=d.end();it++)
cout << *it << " ";
cout << endl;
d.insert(d.end(),22);
d.insert(d.end(), 3,88);
int a[5] = {1,2,3,4,5};
d.insert(d.begin(),a,a+3);
cout << "insert after:" ;
for(it=d.begin();it!=d.end();it++)
cout << *it << " ";
cout << endl;
//c.erase(pos)删除pos位置的元素c.erase(beg,end)删除区间为[beg,end)的元素
//c.erase(beg,end)删除区间为[beg,end)之间的元素
deque<int> d {1,2,3,4,5};
d.erase(d.begin());
deque<int>::iterator it;
cout << "erase(pos) after:" ;
for(it=d.begin();it!=d.end();it++)
cout << *it << " ";
cout << endl;
d.erase(d.begin(), d.begin()+3);
cout << "erase(beg,end) after:" ;
for(it=d.begin();it!=d.end();it++)
cout << *it << " ";
cout << endl;
//c.push_back(num)在末尾位置插入元素
//c.pop_back()删除末尾位置的元素
//c.push_front(num)在开头位置插入元素
//c.pop_front()删除开头位置的元素
deque<int> d {1,2,3,4,5};
d.push_back(10);
deque<int>::iterator it;
cout << "push_back(num):" ;
for(it=d.begin();it!=d.end();it++)
cout << *it << " ";
cout << endl;
d.pop_back();
cout << "pop_back(num):" ;
for(it=d.begin();it!=d.end();it++)
cout << *it << " ";
cout << endl;
d.push_front(10);
cout << "push_front(num):" ;
for(it=d.begin();it!=d.end();it++)
cout << *it << " ";
cout << endl;
d.pop_front();
cout << "pop_front(num):" ;
for(it=d.begin();it!=d.end();it++)
cout << *it << " ";
cout << endl;
//c.resize(num)从新定义容器的大小
deque<int> d {1,2,3,4,5};
cout << "d.size():" << d.size() << endl;
d.resize(d.size()+5);
cout << "d.resize() after:" << d.size() <<endl;
deque<int>::iterator it;
cout << "resize() after:" ;
for(it=d.begin();it!=d.end();it++)
cout << *it << " ";
cout << endl;
//c1.swap(c2)交换容器c1,c2;
//swap(c1,c2)同上。
deque<int> d1 {1,2,3,4,5},d2,d3;
d1.swap(d2);
deque<int>::iterator it;
cout << "d1 swap after:" ;
for(it=d1.begin();it!=d1.end();it++)
cout << *it << " ";
cout << endl;
cout << "d2 swap after:" ;
for(it=d2.begin();it!=d2.end();it++)
cout << *it << " ";
cout << endl;
swap(d3,d2);
cout << "d3 swap after:" ;
for(it=d3.begin();it!=d3.end();it++)
cout << *it << " ";
cout << endl;
/*重载运算符
operator==
operator!=
operator<
operator<=
operator>
operator>=
*/
//引自于http://www.cnblogs.com/scandy-yuan/archive/2013/01/09/2853603.html
//http://blog.csdn.net/morewindows/article/details/6950917</span>
deque双向队列
最新推荐文章于 2024-03-27 19:00:15 发布