1.构造函数
#include <iostream>
using namespace std;
#include <deque>
void printfDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
//*it = 100; //容器中数据不可修改
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int >d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printfDeque(d1);
deque<int>d2(d1.begin(), d1.end());
printfDeque(d2);
deque<int>d3(10, 100);
printfDeque(d3);
deque<int>d4(d3);
printfDeque(d4);
}
int main()
{
test01();
}
2. 赋值操作
#include <iostream>
using namespace std;
#include <deque>
void printfDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
//*it = 100; //容器中数据不可修改
cout << *it << " ";
}
cout << endl;
}
void test01()
{
//重载等号运算符
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
deque<int>d2 = d1;
printfDeque(d2);
//将[begin,end)区间中的数据拷贝赋值给本身
deque<int>d3;
d3.assign(d1.begin(), d1.end());
printfDeque(d3);
//将n个elem拷贝赋值给本身
deque<int>d4;
d4.assign(10, 100);
printfDeque(d4);
}
int main()
{
test01();
}
3.大小操作
#include <iostream>
using namespace std;
#include <deque>
void printfDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
//*it = 100; //容器中数据不可修改
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printfDeque(d1);
if (d1.empty())
{
cout << "空" << endl;
}
else
{
cout << "不为空" << endl;
}
cout << d1.size() << endl;
d1.resize(15);
printfDeque(d1);
d1.resize(20, 20);
printfDeque(d1);
}
int main()
{
test01();
}
4. 插入和删除
#include <iostream>
using namespace std;
#include <deque>
void printfDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
//*it = 100; //容器中数据不可修改
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int>d1;
d1.push_back(10);
d1.push_back(20);
d1.push_front(1);
d1.push_front(2);
d1.pop_back();
d1.pop_front();
printfDeque(d1);
d1.insert(d1.begin(),10);//位置需用迭代器,不能用下标
printfDeque(d1);
d1.insert(d1.begin() + 1, 10, 100);
printfDeque(d1);
d1.insert(d1.begin() + 1, d1.begin(), d1.end());
printfDeque(d1);
d1.clear();
printfDeque(d1);
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
//erase的返回值为删除值下一个数据位置
d1.erase(d1.begin());
printfDeque(d1);
d1.erase(d1.begin(),d1.end());
printfDeque(d1);
}
int main()
{
test01();
}
5.数据存取
和vector几乎一样
多提供了front() back()
#include <iostream>
using namespace std;
#include <deque>
void printfDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
//*it = 100; //容器中数据不可修改
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int>d1;
d1.push_back(10);
d1.push_back(20);
d1.push_front(1);
d1.push_front(2);
d1.pop_back();
d1.pop_front();
printfDeque(d1);
cout << d1[0] << endl;
cout << d1.at(1) << endl;
cout << d1.front() << endl;
cout << d1.back() << endl;
}
int main()
{
test01();
}
6.排序操作
#include <iostream>
using namespace std;
#include <deque>
#include <algorithm>
void printfDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
//*it = 100; //容器中数据不可修改
cout << *it << " ";
}
cout << endl;
}
//排序操作
void test01()
{
deque<int>d1;
d1.push_back(10);
d1.push_back(20);
d1.push_front(1);
d1.push_front(2);
printfDeque(d1);
//支持随机访问的迭代器的容器,都可以利用sort算法直接进行排序
// vector deque
//排序规则,从小到大
sort(d1.begin(), d1.end());
printfDeque(d1);
}
int main()
{
test01();
}