STL - 常用顺序容器代码

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: 不多说,看代码 #include #include #include #include #include #include "ContainerTest.h" #include "ContainerUtil.

不多说,看代码

#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
 
 

目录
相关文章
|
3月前
|
存储 算法 C++
STL几个容器的比较
STL几个容器的比较
|
4月前
|
存储 C++ 容器
【C++】STL容器——vector类的使用指南(含代码演示)(11)
【C++】STL容器——vector类的使用指南(含代码演示)(11)
|
4月前
|
设计模式 C++ iOS开发
【C++】STL容器适配器入门:【堆】【栈】【队列】(16)
【C++】STL容器适配器入门:【堆】【栈】【队列】(16)
【C++】STL容器适配器入门:【堆】【栈】【队列】(16)
|
1月前
|
存储 安全 C++
深入理解C++ STL中的vector容器
深入理解C++ STL中的vector容器
11 0
|
2月前
|
存储 算法 C++
万字长文:C++模板与STL【常用STL容器】
万字长文:C++模板与STL【常用STL容器】
|
2月前
|
存储 算法 C++
C++ STL精通之旅:向量、集合与映射等容器详解
C++ STL精通之旅:向量、集合与映射等容器详解
97 0
|
2月前
|
存储 前端开发 C++
【C++入门到精通】C++入门 —— 容器适配器、stack和queue(STL)
在C++中​​std::stack​​​是一个模板类,它是基于容器的适配器,用于实现堆栈数据结构。堆栈是一种后进先出(LIFO)的数据结构,类似于现实生活中的一叠盘子。
27 4
|
2月前
|
存储 定位技术 C++
C++ STL容器与常用库函数
C++ STL容器与常用库函数
70 0
C++ STL容器与常用库函数
|
3月前
|
存储 C++ 容器
|
3月前
|
C++ 容器
stl容器笔记
stl容器笔记