代码如下:
#include <iostream>
#include <string>
#include <deque>
using namespace std;void printDeque(const deque<int>& d)
{for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{deque<int>d1;for (int i = 0; i < 10; i++){d1.push_back(i);}printDeque(d1);deque<int>d2(d1.begin(), d1.end());printDeque(d2);deque<int>d3(10, 100);printDeque(d3);deque<int>d4 = d3;printDeque(d4);
}int main()
{test01();return 0;
}
测试结果:
总结:
deque容器和vector容器的构造方式几乎一致,灵活使用即可。
代码如下:
#include <iostream>
#include <string>
#include <deque>
using namespace std;void printDeque(const deque<int>& d)
{for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{deque<int>d1;for (int i = 0; i < 10; i++){d1.push_back(i);}printDeque(d1);deque<int>d2;d2 = d1;printDeque(d2);deque<int>d3;d3.assign(d1.begin(), d1.end());printDeque(d3);deque<int>d4;d4.assign(10, 100);printDeque(d4);
}int main()
{test01();return 0;
}
测试结果:
代码如下:
#include <iostream>
#include <string>
#include <deque>
using namespace std;void printDeque(const deque<int>& d)
{for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{deque<int>d1;for (int i = 0; i < 10; i++){d1.push_back(i);}printDeque(d1);if (d1.empty()){cout << "d1 empty" << endl;}else{cout << "d1 no empty" << endl;cout << "d1 size = " << d1.size() << endl;}d1.resize(15, 1);printDeque(d1);d1.resize(5);printDeque(d1);}int main()
{test01();return 0;
}
测试结果:
总结:
代码如下:
#include <iostream>
#include <string>
#include <deque>
using namespace std;void printDeque(const deque<int>& d)
{for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{deque<int>d;d.push_back(10);d.push_back(20);d.push_front(100);d.push_front(200);printDeque(d);d.pop_back();d.pop_front();printDeque(d);
}void test02()
{deque<int>d;d.push_back(10);d.push_back(20);d.push_front(100);d.push_front(200);printDeque(d);d.insert(d.begin(), 1000);printDeque(d);d.insert(d.begin(), 2, 10000);printDeque(d);deque<int>d2;d2.push_back(1);d2.push_back(2);d2.push_back(3);d.insert(d.begin(), d2.begin(), d2.end());printDeque(d);
}int main()
{test01();test02();return 0;
}
测试结果:
总结:
代码如下:
#include <iostream>
#include <string>
#include <deque>
using namespace std;void printDeque(const deque<int>& d)
{for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{deque<int>d;d.push_back(10);d.push_back(20);d.push_front(100);d.push_front(200);for (int i = 0; i < d.size(); i++){cout << d[i] << " ";}cout << endl;for (int i = 0; i < d.size(); i++){cout << d.at(i) << " ";}cout << endl;cout << "front = " << d.front() << endl;cout << "back = " << d.back() << endl;
}int main()
{test01();return 0;
}
测试结果:
总结:
代码如下:
#include <iostream>
#include <string>
#include <deque>
#include <algorithm>
using namespace std;void printDeque(const deque<int>& d)
{for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{deque<int>d;d.push_back(10);d.push_back(20);d.push_front(100);d.push_front(200);printDeque(d);sort(d.begin(), d.end());printDeque(d);
}int main()
{test01();return 0;
}
测试结果:
总结: