1、end——容器迭代器最后一个元素的下一个迭代
#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.end()-1; cout << *It << endl; return 0; } 运行结果: // 1 2 3 4 5 // 5
2、erase——从容器中删除某个元素或者某个区间
#include <iostream> #include <deque> #include <iterator> #include <algorithm> using namespace std; int main () { deque<int> d(10); deque<int>::iterator It; for ( int i=0; i<10; i++ ) d[i] = i+1; copy(d.begin(),d.end(), ostream_iterator<int>(cout," ")); cout << endl; It = d.begin()+2; // 删除容器的第三个元素 d.erase(It); copy(d.begin(),d.end(), ostream_iterator<int>(cout," ")); cout << endl; It = d.begin(); // 删除容器开头的两个元素 d.erase(It,It+2); copy(d.begin(),d.end(), ostream_iterator<int>(cout," ")); cout << endl; return 0; } 运行结果: // 1 2 3 4 5 6 7 8 9 10 // 1 2 4 5 6 7 8 9 10 // 4 5 6 7 8 9 10
3、front——返回容器的第一个元素
#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("Linda",75000)); d.push_back(M("Robert",60000)); deque<M>::iterator It = d.begin(); cout << "Entire deque:" << endl; while ( It != d.end() ) (It++)->print(); cout << endl; cout << "Return from front()" << endl; d.front().print(); return 0; } 运行结果: // Entire deque: // Linda 75000 // Robert 60000 // // Return from front() // Linda 75000
4、insert——向容器中插入一个、多个或者一组元素
#include <iostream> #include <deque> #include <iterator> #include <algorithm> using namespace std; template <class T> class Print { public: void operator () (T& t) { cout << t << " "; } }; //============================= int main () { int ary[5]; fill(ary,ary+5,1); deque<int> d; deque<int>::iterator It; Print<int> print; copy(ary,ary+5, back_inserter(d)); cout << "deque d: "; for_each(d.begin(),d.end(),print); cout << endl; It = d.begin(); // 将"5" 插入"It" 位置 cout << "d.insert(It,5): "; d.insert(It,5); for_each(d.begin(),d.end(),print); cout << endl; // 将区间[ary+2 - ary+5]插入"It" 位置 It = d.begin()+5; cout << "d.insert(It,ary+2,ary+5 : "; d.insert(It,ary+2,ary+5); for_each(d.begin(),d.end(),print); cout << endl; // 将两个 "20" 插入 "It" 位置 It = d.end()-2; cout << "d.insert(It,2,20) : "; d.insert(It,2,20); for_each(d.begin(),d.end(),print); cout << endl; return 0; } 运行结果: // deque d : 1 1 1 1 1 // d.insert(It,5) : 5 1 1 1 1 1 // d.insert(It,ary+2,ary+5 : 5 1 1 1 1 1 1 1 1 // d.insert(It,2,20) : 5 1 1 1 1 1 1 20 20 1 1
5、max_size——容器能申请到的最大容量
#include <iostream> #include <deque> using namespace std; int main () { deque<int> d(10); cout << "Size of d = " << d.size() << endl; cout << "Max_size of d = " << d.max_size() << endl; return 0; } 运行结果: // Size of d = 10 // Max_size of d = 1073741823
6、pop_back——让容器的最后一个元素出栈
#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; Print<int> print; for ( int i=0; i<5; i++ ) d.push_back(i+1); while ( !d.empty() ) { for_each(d.begin(),d.end(),print); cout << endl; d.pop_back(); } return 0; } 运行结果: // 1 2 3 4 5 // 1 2 3 4 // 1 2 3 // 1 2 // 1
7、pop_front——让容器的第一个元素出栈
#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; Print<int> print; for ( int i=0; i<5; i++ d.push_back(i+1); while ( !d.empty() ) { for_each(d.begin(),d.end(),print); cout << endl; d.pop_front(); } return 0; } 运行结果: // 1 2 3 4 5 // 2 3 4 5 // 3 4 5 // 4 5 // 5
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。