跳到正文

个人记录

stl

diu·约 2 分钟
原文
文章信息与版本

文章信息

发布
2018年11月1日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

常用

  • map

    (数据有序且自动去重)

格式:

C++
    # include <map>

      map<int,int>x;
  • stack

    (先进后出)

格式:

C++
	# include <stack>

      stack<int>x;
  • queue

    (先进先出)

格式:

C++
    # include <queue>

     queue<int>x;
  • priority_queue

    (头为数据最大 or 最小)

格式:

C++
    # include <queue>

     priority_queue<int>x;

容器

  • vector

    (支持下标访问,从尾部插入更快)

  • set

    (数据有序)

  • list

    (支持任意位置插入or删除,但只能有迭代器)

  • deque

    (支持下标访问,头尾插入均快)


操作

  • x.push_back()

    (在数组尾部添加一个数据)

  • x.pop_back()

    (去掉数组最后一个数据,长度减1)

  • x.front()

    (返回数据栈顶元素)

  • x.begin()

    (得到数据开头数据)

  • x.end()

    (数据结尾的下一个数据)

  • x.clear()

    (移除容器内所有数据)

  • x.empty()

    (判断容器是否为空)

  • x.erase(pos)

    (删除pos位置的数据)

  • x.erase(beg,end)

    (删除 beg-end 此区间内的数据)

  • x.size()

    (容器中数据的个数)

  • x.insert(pos,data)

    (在pos处插入数据data)

  • x.top()

    (stack和priority_queue取栈顶元素)

  • string 类型

  • x.substr(开始位置,子串长度len);

    (提取子串)

  • x.replace(开始位置,长度len,需要换上的子串p);

    (替换子串)


迭代器

  • 理解为指针即可

  • 初始化:

    C++
    (迭代器类型-即map之类的)
    
    <int(放数据类型)>::iterator  it(名字);
    

    例: vector<int>::iterator it;

  • 反向迭代器:

    C++
    (迭代器类型-即map之类的)
    
    <int(放数据类型)>::iterator  it(名字);

    例: vector<int>::reverse_iterator it;

原文