1、构造函数
#include <iostream> #include <deque> #include <string> #include <algorithm> using namespace std; int main () { string str[]={"Alex","John","Robert"}; // 新建一个空的deque对象 deque<int> d1; // 新建一个包含10个元素的deque对象 deque<int> d2(10); // 新建一个包含10个元素的deque对象,并给每个元素赋初始值0 deque<int> d3(10,0); // 新建一个deque对象并用指定区间给元素赋值 deque<string> d4(str+0,str+3); deque<string>::iterator sIt = d4.begin(); while ( sIt != d4.end() ) cout << *sIt++ << " "; cout << endl; // 拷贝构造 deque<string> d5(d4); for ( int i=0; i<3; i++ ) cout << d5[i] << " "; cout << endl; return 0; } 运行结果: // Alex John Robert // Alex John Robert
2、assign——初始化或者重置容器内元素
#include <iostream> #include <deque> #include <algorithm> #include <iterator> using namespace std; int main () { int ary[]={1,2,3,4,5}; deque<int> d; // 将 "ary" 数组成员分配给 "d" 对象 d.assign(ary,ary+5); copy(d.begin(),d.end(), ostream_iterator<int>(cout," ")); cout << endl; // 用3份100替换d对象内元素 d.assign(3,100); copy(d.begin(),d.end(), ostream_iterator(cout," ")); cout << endl; return 0; } 运行结果: // 1 2 3 4 5 // 100 100 100
3、at——按照指定偏移量访问容器元素,类似于[]运算符
#include <iostream> #include <deque> using namespace std; int main () { deque<int> d(3,0); d[0] = 100; d.at(1) = 200; for ( int i=0; i<3; i++ ) cout << d.at(i) << " "; cout << endl; return 0; } 运行结果: // 100 200 0
4、back——返回容器的最后一个元素值,等价于*(容器.end()-1)或容器[容器.size-1]
#include <iostream> #include <deque> #include <string> #include <iterator> using namespace std; template<class T, class D> class Member { public: Member(T t, D d) : name(t), sal(d) {} void print(); private: T name; D sal; }; template<class T, class D> void Member::print() { cout << name << " " << sal << endl; } //====================================== int main () { typedef Member<string,double> M; deque<M> d; d.push_back(M("Robert",60000)); d.push_back(M("Linda",75000)); deque<M>::iterator It = d.begin(); cout << "Entire deque:" << endl; while ( It != d.end() ) (It++)->print(); cout << endl; cout << "Return from back()" << endl; d.back().print(); return 0; } 运行结果: // Entire deque: // Robert 60000 // Linda 75000 // // Return from back() // Linda 75000
5、begin——返回容器头
#include <iostream> #include <deque> #include <iterator> #include <numeric> using namespace std; int main () { deque<int> d(5); iota(d.begin(),d.end(),1); deque<int>::iterator It = d.begin(); while ( It != d.end() ) cout << *It++ << " "; cout << endl; // deque的第三个元素 It = d.begin()+2; cout << *It << endl; return 0; } 运行结果: // 1 2 3 4 5 // 3
6、clear——容器就像一个盒子,clear便是拿走盒子里面的东西
#include <iostream> #include <deque> #include <algorithm> using namespace std; template <class T> class Print { public: void operator () (T& t) { cout << t << " "; } }; //============================== int main () { deque<int> d(10); Print<int> print; fill(d.begin(),d.end(),5); cout << "Deque d : "; for_each(d.begin(),d.end(),print); cout << endl; cout << "Size of d = " << d.size() << endl; cout << "d.clear" << endl; d.clear(); cout << "Deque d : "; for_each(d.begin(),d.end(),print); cout << endl; cout << "Size of d = " << d.size() << endl; cout << "Deque d is "; d.empty() ? cout << "" : cout << "not "; cout << "empty" << endl; return 0; } 运行结果: // Deque d : 5 5 5 5 5 5 5 5 5 5 // Size of d = 10 // d.clear // Deque d : // Size of d = 0 // Deque d is empty
7、empty——容器就像一个盒子,empty便是判断盒子里面是否有东西
#include <iostream> #include <deque> using namespace std; int main () { deque<int> d; cout << "Deque is "; d.empty() ? cout << "" : cout << "not "; cout << "empty" << endl; d.push_back(100); cout << "Deque is "; d.empty() ? cout << "" : cout << "not "; cout << "empty" << endl; return 0; } // Deque is empty // Deque is not empty
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。