#include<iostream>
#include<deque>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
deque<string> coll;
coll.assign(3, string("string"));
coll.push_back("last string");
coll.push_front("first string");
copy(coll.begin(), coll.end(),
ostream_iterator<string>(cout, "\n"));
coll.pop_front();
coll.pop_back();
for (int i = 1; i < coll.size(); i++)
{
coll[i] = "another " + coll[i];
}
coll.resize(4, "resized string");
copy(coll.begin(), coll.end(), ostream_iterator<string>(cout, "\n"));
getchar();
getchar();
#include<deque>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
deque<string> coll;
coll.assign(3, string("string"));
coll.push_back("last string");
coll.push_front("first string");
copy(coll.begin(), coll.end(),
ostream_iterator<string>(cout, "\n"));
coll.pop_front();
coll.pop_back();
for (int i = 1; i < coll.size(); i++)
{
coll[i] = "another " + coll[i];
}
coll.resize(4, "resized string");
copy(coll.begin(), coll.end(), ostream_iterator<string>(cout, "\n"));
getchar();
getchar();
}
以下为输出的结果:
first string
string
string
string
last string
string
another string
another string
resized string
deque与vector相比之下有几点不同之处
1.deque能够快速的在首部和尾部安插元素,这些操作都分摊在常熟时间内。
2.存取元素的时候deque的内部结构会多一个间接地过程,所以元素的存取和迭代器的动作会稍微慢一些
3.迭代器需要在不同的块之间跳转,所以必须是特殊的智能指针,非一般指针。
4.在对内存块有所限制的系统中,deque可以内含更多的元素,因为他不止一块内存,因为deque的max_size可能更大。
5.当deque的内存块不在使用的时候,会被释放,deque的内存大小是可缩减的,不过实际情况由版本决定。
6.迭代器属于随机存取迭代器。
在以下的情况下使用deque
1.你需要在两端安插或者移除元素。
2.无需引用容器内的元素。
3.要求容器释放不再使用的元素。