CPP_STL

一、概述

STL(标准模板库),是目前C++内置支持的library,底层利用了C++类模板和函数模板的机制,广义上由三大部分组成:容器、算法和迭代器。

STL大体分为六大组件,分别是容器、算法、迭代器、仿函数、适配器(配接器)、空间配置器。

  • 容器:各种数据结构,如vector、list、deque、set、map等,用来存放数据。
  • 算法:各种常用的算法,如sort、find、copy、for_each等。
  • 迭代器:扮演了容器与算法之间的胶合剂。
  • 仿函数:行为类似函数,可作为算法的某种策略。
  • 适配器:一种用来修饰容器或者仿函数或迭代器接口的东西。
  • 空间配置器:负责空间的配置与管理。

容器是STL中很重要的一种数据结构。常见的容器包括:

  • vector容器
  • stack栈模型
  • deque双端数组,queue队列模型
  • list链表模型
  • priotriy_queue优先级队列
  • set与multiset容器
  • map与multimap容器

stl_container

在线手册:http://www.martinbroadhurst.com/stl/

man:Ubuntu16下sudo apt-get install libstdc++-5-doc, man std::vector

queue

cppreference-queue

queue是单向队列数据结构,具备对头和队尾,FIFO队列。

#include <queue>
template<
    class T,
    class Container = std::deque<T>
> class queue;
// element access
reference front();  // access the first element
const_reference front() const;
reference back(); // access the last element
const_reference back() const;

// capacity
bool empty() const;
size_type size() const;

// modifiers
void push(const value_type& __x); // inserts element at the end
void pop();  //remove the first element
class Teacher
{
public:
    int age;
    char name[32];
    
    void printT(){
        cout << "age :" << age <<endl;
    }
}

void main()
{
    Teacher t1,t2,t3;
    t1.age = 31;
    t2.age = 32;
    t3.age = 33;
    
    queue<Teacher > q;
    q.push(t1);
    q.push(t2);
    q.push(t3);
    
    while (!q.empty()){
        Teacher tmp = q.front();
        tmp.printT();
        q.pop();
    }
}

参考

  1. C++提高编程(一) 匠心之作 从0到1入门学编程 黑马程序员
  2. C++进阶-STL容器,你看我就够了 - 简书
posted @ 2018-10-07 09:00  yuxi_o  阅读(306)  评论(0编辑  收藏  举报