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
     
     

  • 相关阅读:
    一个基础的CURL类
    设计自适应网页方法
    JQ点击列表显示隐藏
    获取当前页面的完整URL
    配置时间生成下拉菜单
    Contains Duplicate II
    Rectangle Area
    面试题47:不用加减乘除做加法
    面试题48:用C++设计一个不能被继承的类
    Reverse Linked List
  • 原文地址:https://www.cnblogs.com/davidgu/p/4737090.html
Copyright © 2011-2022 走看看