STL

不多说,看代码

#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include "ContainerTest.h"
#include "ContainerUtil.h"

using namespace std;

void ContainerTest::run()
{
    /*
        1. vector test
    */

    vector<int> coll;
    for (int i = 1; i <= 6; ++i)
    {
        coll.push_back(i);
    }
    cout << "** print elements of vector **" << endl;
    ContainerUtil<vector<int>>::printElements(coll);

    /*
        2. deque test
    */

    deque<int> coll2;
    for (int i = 1; i <= 6; ++i)
    {
        coll2.push_front(i);
    }
    cout << "** print elements of deque **" << endl;
    ContainerUtil<deque<int>>::printElements(coll2);

    /*
        3. list test
    */

    list<char> coll3;
    for (char c = 'a'; c <= 'z';++c)
    {
        coll3.push_back(c);
    }
    cout << "** print elements of list **" << endl;
    ContainerUtil<list<char>>::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<long> 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<forward_list<long>>::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  z
print 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
 
 

原文地址:https://www.cnblogs.com/davidgu/p/4737090.html