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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值