1、push_back——将某个元素从容器后端入栈
#include <iostream>
#include <deque>
#include <string>
#include <iterator>
using namespace std;
template <class T>
class Name
{
public:
Name(T t) : name(t) {}
void print()
{
cout << name << " ";
}
private:
T name;
};
//=============================
int main ()
{
typedef Name<string> N;
typedef deque<N> D;
D d;
N n1("Robert");
N n2("Alex");
d.push_back(n1);
d.push_back(n2);
// unnamed object of the type Name
d.push_back(N("Linda"));
D::iterator It = d.begin();
while ( It != d.end() )
(It++)->print();
cout << endl;
return 0;
}
运行结果:
// Robert Alex Linda
2、push_front——将某个元素从容器头入栈
#include <iostream>
#include <deque>
#include <string>
#include <iterator>
using namespace std;
template <class T>
class Name
{
public:
Name(T t) : name(t) {}
void print()
{
cout << name << " ";
}
private:
T name;
};
//=============================
int main ()
{
typedef Name<string> N;
typedef deque<N> D;
D d;
N n1("Robert");
N n2("Alex");
d.push_front(n1);
d.push_front(n2);
d.push_front(N("Linda"));
D::iterator It = d.begin();
while ( It != d.end() )
(It++)->print();
cout << endl;
return 0;
}
运行结果:
// Linda Alex Robert
3、rbegin、rend——容器逆序输出的头和尾
#include <iostream>
#include <iomanip>
#include <deque>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
class ID
{
friend bool operator < ( const ID&, const ID& );
public:
ID(string name,int score) : name(name), score(score) {}
void display ()
{
cout.setf(ios::left);
cout << setw(3) << score << name << endl;
}
private:
string name; int score;
};
//-----------------------------------------------------
// 比较排序
bool operator < ( const ID& a, const ID& b )
{
return a.score < b.score;
}
//-----------------------------------------------------
typedef deque<ID> Deque;
int main ()
{
Deque d;
Deque::iterator Iter;
d.push_back(ID("Smith A",96));
d.push_back(ID("Amstrong B.",91));
d.push_back(ID("Watson D.",82));
for ( Iter = d.begin(); Iter != d.end(); Iter++ )
Iter->display();
sort(d.begin(),d.end()); // 排序算法
cout << endl << "Sorted by Score" << endl;
cout << "===============" << endl;
for ( Iter = d.begin(); Iter != d.end(); Iter++ )
Iter->display();
cout << endl << "Reverse output" << endl;
cout << "===============" << endl;
Deque::reverse_iterator r = d.rbegin();
while ( r != d.rend() )
cout << r->display();
cout << endl;
return 0;
}
运行平台:
// 96 Smith A.
// 91 Amstrong B.
// 82 Watson D.
//
// Sorted by Score
// ===============
// 82 Watson D.
// 91 Amstrong B.
// 96 Smith A.
//
// Reverse output
// ===============
// 96 Smith A.
// 91 Amstrong B.
// 82 Watson D.
4、resize——减小或者扩大容器的容量
#include <iostream>
#include <deque>
#include <algorithm>
#include <iterator>
using namespace std;
int main ()
{
deque<int> d(5);
for ( int i=0; i<5; i++ )
d[i] = i*2;
copy(d.begin(),d.end(),
ostream_iterator<int>(cout," "));
cout << endl;
d.resize(7,100);
copy(d.begin(),d.end(),
ostream_iterator<int>(cout," "));
cout << endl;
d.resize(4);
copy(d.begin(),d.end(),
ostream_iterator<int>(cout," "));
cout << endl;
return 0;
}
运行结果:
// 0 2 4 6 8
// 0 2 4 6 8 100 100
// 0 2 4 6
5、size——返回容器内元素的个数
#include <iostream>
#include <deque>
#include <algorithm>
#include <iterator>
using namespace std;
template <class T>
class Print
{
public:
void operator () (T& t)
{
cout << t << " ";
}
};
//=============================
int main ()
{
deque<char> d(5);
Print<char> print;
cout << "Size of d = " << d.size() << endl;
fill(d.begin(),d.end(),'*');
for_each(d.begin(),d.end(),print);
cout << endl;
for ( int i=0; i < d.size(); i++ )
cout << d[i] << " ";
cout << endl;
for ( int i=0; i<5; i++ )
{
cout << "Size of d = ";
for_each(d.begin(),d.end(),print);
cout << endl;
d.pop_back();
}
return 0;
}
运行结果:
// Size of d = 5
// * * * * *
// * * * * *
// Size of d = * * * * *
// Size of d = * * * *
// Size of d = * * *
// Size of d = * *
// Size of d = *
6、swap——将自身容器的内容与指定容器的内容交换
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
template <class T>
class Print
{
public:
void operator () (T& t)
{
cout << t << " ";
}
};
//=============================
int main ()
{
int ary[] = {1,2,3,4,5,6,7,8,9,10};
Print print;
deque<int> d1(ary,ary+7);
deque<int> d2(ary+7,ary+10);
cout << "Deque d1 : ";
for_each(d1.begin(),d1.end(),print);
cout << endl;
cout << "Size of d1 = " << d1.size()
<< endl << endl;
cout << "Deque d2 : ";
for_each(d2.begin(),d2.end(),print);
cout << endl;
cout << "Size of d2 = " << d2.size()
<< endl << endl;
d1.swap(d2);
cout << "After swapping:" << endl;
cout << "Deque d1 : ";
for_each(d1.begin(),d1.end(),print);
cout << endl;
cout << "Size of d1 = " << d1.size()
<< endl << endl;
cout << "Deque d2 : ";
for_each(d2.begin(),d2.end(),print);
cout << endl;
cout << "Size of d2 = " << d2.size()
<< endl << endl;
return 0;
}
运行结果:
// Deque d1 : 1 2 3 4 5 6 7
// Size of d1 = 7
//
// Deque d2 : 8 9 10
// Size of d2 = 3
//
// After swapping:
// Deque d1 : 8 9 10
// Size of d1 = 3
//
// Deque d2 : 1 2 3 4 5 6 7
// Size of d2 = 7
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。



