不多说,看代码
#include#include #include #include #include
#include "ContainerTest.h"#include "ContainerUtil.h"using namespace std;void ContainerTest::run(){ /* 1. vector test */ vector coll; for (int i = 1; i <= 6; ++i) { coll.push_back(i); } cout << "** print elements of vector **" << endl; ContainerUtil >::printElements(coll); /* 2. deque test */ deque coll2; for (int i = 1; i <= 6; ++i) { coll2.push_front(i); } cout << "** print elements of deque **" << endl; ContainerUtil >::printElements(coll2); /* 3. list test */ list coll3; for (char c = 'a'; c <= 'z';++c) { coll3.push_back(c); } cout << "** print elements of list **" << endl; ContainerUtil
>::printElements(coll3); cout << "print again:" << endl; while (!coll3.empty()) { cout << coll3.front() << ' '; coll3.pop_front(); } cout << endl; /* 4. forward list */ // create forward-list container for some prime numbers forward_list coll4 = { 2, 3, 5, 7, 11, 13, 17 }; // resize two times // - note: poor performance coll4.resize(9); coll4.resize(10, 99); cout << "** print elements of forward list **" << endl; ContainerUtil >::printElements(coll4);}
运行结果:
** print elements of vector **
1 2 3 4 5 6** print elements of deque ** 6 5 4 3 2 1** print elements of list ** a b c d e f g h i j k l m n o p q r s t u v w x y zprint again:a b c d e f g h i j k l m n o p q r s t u v w x y z** print elements of forward list ** 2 3 5 7 11 13 17 0 0 99