zoukankan      html  css  js  c++  java
  • 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
     
     

  • 相关阅读:
    asp.net 2.0 国际化 动态切换语言
    SKU、UPC、EAN和ISBN
    NCalc:处理数学运算的好帮手
    yaf 论坛安装
    Afterlogic xmail 邮局软件不能收email 设置
    要围着中心来做事
    保证Winform程序只有一个实例在运行
    可视热敏读写卡开发
    jQuery CSS 效果
    代碼小片斷
  • 原文地址:https://www.cnblogs.com/davidgu/p/4737090.html
Copyright © 2011-2022 走看看