c++STL

vector

vector, 变长数组,倍增的思想

  • size() 返回元素个数
  • empty() 返回是否为空
  • clear() 清空
  • front()/back()
  • push_back()/pop_back()
  • begin()/end()
    支持比较运算,按字典序
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> a;
vector<int>b(4, 3), c(3, 4);//初始化b:3 3 3 3 c:4 4 4 第一个参数是数目,第二个参数是要初始化的值
if (b < c) puts("b < c");
for (int i = 0; i < 10; i++) a.push_back(i);
for (int i = 0; i < a.size(); i++) cout << a[i] << " ";
cout << endl;
for (vector<int>::iterator i = a.begin(); i != a.end(); i++) cout << *i << " ";
cout << endl;
for (auto i = a.begin(); i != a.end(); i++) cout << *i << " ";
cout << endl;
for (auto x : a) cout << x << " ";
cout << endl;
return 0;
}

输出:

b < c
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9

pair

pair<int, int>

  • first 第一个元素
  • second 第二个元素
    支持比较运算,以first为第一关键字,以second为第二关键字(字典序)
#include <bits/stdc++.h>
using namespace std;
int main() {
pair<int, string> p;
p = make_pair(10, "ljz");
p = {20, "abc"};
pair<int, pair<int, int>> p1;//存储三个量可以这样
return 0;
}

string,字符串

  • size()/length() 返回字符串长度
  • empty()
  • clear()
  • substr(起始下标,(子串长度)) 返回子串
  • c_str() 返回字符串所在字符数组的起始地址
#include <bits/stdc++.h>
using namespace std;
int main() {
string a = "ljz";
a += "abc";
a += 'c';
cout << a << "\n";
cout << a.substr(1, 2) << "\n";
cout << a.substr(1, 10) << "\n";
cout << a.substr(1) << "\n";
printf("%s\n", a.c_str());
return 0;
}
/*
输出:
ljzabcc
jz
jzabcc
jzabcc
ljzabcc
*/

queue,队列

  • size()
  • empty()
  • push() 向队尾插入一个元素
  • front() 返回队头元素
  • back() 返回队尾元素
  • pop() 弹出队头元素

priority_queue,优先队列,默认是大根堆

  • size()
  • empty()
  • push() 插入一个元素
  • top() 返回堆顶元素
  • pop() 弹出堆顶元素
  • 定义成小根堆的方式:priority_queue<int, vector<int>, greater<int>> q;

stack,栈

  • size()
  • empty()
  • push()
  • top()
  • pop()

deque,双端队列

  • size()
  • empty()
  • clear()
  • front()/back()
  • push_back()/pop_back()
  • push_front()/pop_front()
  • begin()/end()

set,map,multiset,multimap

  • size()
  • empty()
  • clear()
  • begin()/end()

set/multiset

  • insert() 插入一个数
  • find() 查找一个数
  • count() 返回某一个数的个数
  • erase()
    (1) 输入是一个数x,删除所有x O(k + logn)
    (2) 输入一个迭代器,删除这个迭代器
  • lower_bound()/upper_bound()
    lower_bound(x) 返回大于等于x的最小的数的迭代器
    upper_bound(x) 返回大于x的最小的数的迭代器

map/multimap

  • insert() 插入的数是一个pair
  • erase() 输入的参数是pair或者迭代器
  • find()
    可以当作数组用
    注意multimap不支持此操作。 时间复杂度是 O(logn)
posted @   csai_H  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示