#include
#include
#include
using namespace std;
/* deque(队列)类容器,#include
//添加移除
void test()
{
deque
d.push_back(2); //在deque容器尾部插入一个数据
d.push_back(0);
d.push_back(1);
d.push_back(8);
d.push_front(5);//在deque容器的首部插入一个数据
d.push_front(0);
for (deque
{
cout << *it;
}
cout << endl;
deque
deque
deque
cout << "d2:";
for (deque
{
cout << *it <<" ";
}
cout << endl;
d.pop_back(); //在deque容器尾部删除一个数据
d.pop_front(); //在deque容器的首部删除一个数据
for (deque
{
cout << *it;
}
cout << endl;
cout << d.at(2) << endl; //返回3位置的数据,如果越界,抛出out_of_range
cout << d.size() << endl;
cout << d[3] << endl; //返回3位置的数据
cout<
deque
it = d.begin(); //返回一个开始迭代器
d.end(); //返回一个末尾迭代器
d.rbegin(); //返回一个末尾的反向迭代器
d.rend(); //返回一个开始的反向迭代器
}
//deque赋值
void test2()
{
deque
deque
deque
for (deque
{
cout << *it << " ";
}
cout << endl;
d3.assign(6, 'A'); //将6个A赋值给d3.相当于=
for (deque
{
cout << *it << " ";
}
cout << endl;
d3.assign(d1.begin(), d1.end()); //将区间内的数据赋值给d3
for (deque
{
cout << *it << " ";
}
cout << endl;
d3.swap(d2); //将容器d3和d2类的数据互换
for (deque
{
cout << *it << " ";
}
cout << endl;
cout << d3.size() << endl; //返回d3的元素个数
d3.resize(10); //将d3的元素个数设置为10个,多了舍弃末尾,少了自动增加,值为默认值
for (deque
{
cout << *it << " ";
}
cout << endl;
d3.resize(15, 'A'); //将d3的元素个数设置为10个,多了舍弃末尾,少了自动增加,值为A
for (deque
{
cout << *it << " ";
}
cout << endl;
d3.clear(); //清空容器d3
if (d3.empty()) //判断d3是否为空,返回bool
{
cout << "d3为空" << endl;
}
else
{
cout << "d3不为空" << endl;
}
}
class A
{
public:
A(string name):name(name){}
public:
string name;
};
//插入
void test3()
{
A a("abcd"), b("vfb");
deque
deque
cout << "插入之前:";
for (deque
{
cout <<(*it).name << " ";
}
cout << endl;
d1.insert(d1.begin(), b);
for (deque
{
cout << (*it1).name << " ";
}
cout << endl;
d1.insert(d1.begin(), 5, b);
for (deque
{
cout << (*it2).name << " ";
}
cout << endl;
d1.insert(d1.begin(), d2.begin(),d2.end());
for (deque
{
cout << (*it3).name << " ";
}
cout << endl;
}
int main()
{
//test();
//test2();
test3();
return 0;
}