C++编程中,常用的算法接口函数的,基本用法的总结,用于算法的刷题。
1. 栈(Stack)
头文件: <stack>
常用操作:
std::stack<int> s;
s.push(x); // 元素 x 入栈
s.pop(); // 弹出栈顶元素(无返回值)
s.top(); // 返回栈顶元素(但不弹出)
s.empty(); // 判断栈是否为空
s.size(); // 返回栈中元素个数
注意: pop() 不返回栈顶元素,需先 top() 再 pop()。
2. 队列(Queue)
头文件: <queue>
常用操作:
std::queue<int> q;
q.push(x); // 元素 x 入队
q.pop(); // 弹出队首元素(无返回值)
q.front(); // 返回队首元素(但不弹出)
q.back(); // 返回队尾元素
q.empty(); // 判断队列是否为空
q.size(); // 返回队列元素个数
注意: pop() 不返回队首元素,需先 front() 再 pop()。
3. 优先队列 / 堆(Priority Queue)
头文件: <queue>
默认是大顶堆(最大堆):
std::priority_queue<int> max_heap;
小顶堆(最小堆)定义方式:
std::priority_queue<int, std::vector<int>, std::greater<int>> min_heap;
常用操作:
pq.push(x); // 插入元素 x
pq.pop(); // 弹出堆顶元素(无返回值)
pq.top(); // 返回堆顶元素(但不弹出)
pq.empty(); // 判断堆是否为空
pq.size(); // 返回堆中元素个数
注意: priority_queue 没有 front() 或 back(),只有 top()。
4. 双端队列(Deque)
头文件: <deque>
常用操作:
std::deque<int> dq;
dq.push_back(x); // 在队尾插入 x
dq.push_front(x); // 在队首插入 x
dq.pop_back(); // 弹出队尾元素(无返回值)
dq.pop_front(); // 弹出队首元素(无返回值)
dq.front(); // 返回队首元素
dq.back(); // 返回队尾元素
dq.empty(); // 判断是否为空
dq.size(); // 返回元素个数
dq[index]; // 随机访问(类似 vector)
5. 算法库(Algorithm)
头文件: <algorithm>
常用函数:
排序
std::sort(v.begin(), v.end()); // 默认升序
std::sort(v.begin(), v.end(), std::greater<int>()); // 降序
查找
auto it = std::find(v.begin(), v.end(), x); // 返回迭代器,找不到返回 v.end()
bool exists = std::binary_search(v.begin(), v.end(), x); // 二分查找(要求有序)
反转
std::reverse(v.begin(), v.end()); // 反转容器
去重(需先排序)
std::sort(v.begin(), v.end());
auto last = std::unique(v.begin(), v.end()); // 返回去重后的尾迭代器
v.erase(last, v.end()); // 删除重复元素
堆操作(基于 vector)
std::make_heap(v.begin(), v.end()); // 建堆(默认大顶堆)
std::pop_heap(v.begin(), v.end()); // 移动堆顶到末尾,需配合 v.pop_back()
std::push_heap(v.begin(), v.end()); // 插入新元素到堆
std::sort_heap(v.begin(), v.end()); // 堆排序
其他
int max_val = std::max(a, b); // 返回较大值
int min_val = std::min(a, b); // 返回较小值
std::swap(a, b); // 交换两个值
6. 集合(Set / Multiset)
头文件: <set>
常用操作:
std::set<int> s; // 有序、不重复
std::multiset<int> ms; // 有序、可重复
s.insert(x); // 插入元素
s.erase(x); // 删除元素
s.find(x); // 查找元素,返回迭代器
s.count(x); // 返回元素出现次数(set 为 0/1)
s.lower_bound(x); // 返回第一个 ≥x 的迭代器
s.upper_bound(x); // 返回第一个 >x 的迭代器
s.empty(); // 判断是否为空
s.size(); // 返回元素个数
7. 映射(Map / Multimap)
头文件: <map>
常用操作:
std::map<std::string, int> m; // key-value 映射
m["key"] = value; // 插入或修改
m.insert({"key", value}); // 插入(如果 key 存在则不覆盖)
m.erase("key"); // 删除
auto it = m.find("key"); // 查找,返回迭代器
m.count("key"); // 返回 key 的数量(0/1)
m.lower_bound("key"); // 返回第一个 key ≥ "key" 的迭代器
m.upper_bound("key"); // 返回第一个 key > "key" 的迭代器
总结
| 数据结构 | 头文件 | 常用操作 |
|---|---|---|
| 栈 | <stack> | push(), pop(), top(), empty(), size() |
| 队列 | <queue> | push(), pop(), front(), back(), empty(), size() |
| 优先队列(堆) | <queue> | push(), pop(), top(), empty(), size() |
| 双端队列 | <deque> | push_back(), push_front(), pop_back(), pop_front(), front(), back() |
| 算法 | <algorithm> | sort(), find(), reverse(), unique(), make_heap() |
| 集合 | <set> | insert(), erase(), find(), count(), lower_bound() |
| 映射 | <map> | operator[], insert(), erase(), find(), count() |
这些是 C++ STL 中最常用的数据结构和算法接口,熟练掌握它们可以大幅提升编程效率!
5003

被折叠的 条评论
为什么被折叠?



