蓝桥杯C++常用STL

小记:

队列、stack都是push

只vector是push_back

set是insert

1. vector(动态数组)

cpp 复制代码
vector<int> v;
v.push_back(1);      // 尾部插入元素(O(1) 均摊)
v.pop_back();        // 删除尾部元素(O(1))

int n;cin >> n;
vector<int> nums;
nums.reserve(n); //预分配n长度空间
vector<int> v1(n,0);//另一种预分配

v.insert(v.begin()+2, 5); // 在指定位置插入(O(n))
v.erase(v.begin()+1);     // 删除指定位置元素(O(n))

v.size();            // 当前元素数量

v.find(v.begin(),v.end(),target);//找到元素对应下标
auto it = std::find(vec.begin(), vec.end(), target);
if (it != vec.end()) {
    std::cout << "找到元素,位置索引: " << std::distance(vec.begin(), it);

v.clear();           // 清空元素(不释放内存)
cpp 复制代码
vector<int> v[N];
//N个一维的vector

2. priority_queue(优先队列,堆)入队则排序

cpp 复制代码
//最小堆,最小元素出列
priority_queue<int,vector<int>,greater<int>>pq;
//默认最大堆,最大元素出列
priority_queue<int>q;//
//左大greater  sort(,greater<int>());

pq.size()>1
ll x=pq.top();pq.pop();//取出第一个队头最小元素,并弹出
ll y=pq.top();pq.pop();//取出当前第一个队头最小元素,并弹出
ans+=x+y;//求和,合并
pq.push(x+y);//合并的部落再入队列,排列后再重复上述操作


priority_queue<int> pq;
pq.push(30);          // 插入元素(O(log n))
pq.pop();             // 删除堆顶元素(O(log n))
pq.top();             // 访问最大元素(O(1))

3. map(键值对映射)

升序,空间小,时间O(logn)

cpp 复制代码
map<string, int> m;
m["apple"] = 5;       // 插入/修改键值对(O(log n))
m.insert({"banana", 3}); //直接插入键值对,一对
auto it = m.find("apple"); // 查找键(O(log n))
m.erase("banana");    // 删除键
m.emplace("cherry", 7); // 直接构造键值对

map遍历:

cpp 复制代码
for(auto val:Map)//遍历map 根据题意出现次数超过一半,值*2>n*m
{
	int current_val = val.second;//每次取出键中的值
	if(current_val*2>n*m)//值出现超过一半
	{
		ans=val.first;//取出其中的键
		break;//跳出
	}
		
}
unordered_map

平均O(1)最坏On 适用于无序、高频查寻 空间占用大

cpp 复制代码
unordered_map<int, int> h;
for (int i = 0; i < n; i ++) {
        cin >> a[i];
        h[a[i]] = i;
    }
h.count(x - b[i])//找键是否存在 x-b[i]是否==a[i]

4.set (集合) 去重+排序

cpp 复制代码
set<pair<int, int>> positions;
positions.insert(simulatePath(steps, i, 'F'));
positions.size();

set<int> s;
s.insert(5);          // 插入元素(O(log n))
s.erase(5);           // 删除元素(O(log n))
s.find(5);            // 查找元素,返回迭代器(O(log n))
s.count(5);           // 存在性检查(0或1)
s.lower_bound(3);     // 第一个 >=3 的元素
s.upper_bound(8);     // 第一个 >8 的元素

sort pair<int,int>默认排序规则

  1. ‌比较 .first‌
    先比较两个 pair 的第一个元素 a.first 和 b.first。
    • 如果 a.first < b.first,则 a 排在 b 前面。
    • 如果 a.first > b.first,则 a 排在 b 后面。
  1. ‌若 .first****相等,再比较 .second‌
    若 a.first == b.first,则比较 a.second 和 b.second:
    • 如果 a.second < b.second,则 a 排在 b 前面。
    • 如果 a.second > b.second,则 a 排在 b 后面。

5.pair (二元组)

cpp 复制代码
vector<pair<int, int>> directions = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
int direction = 0;//
int dr = directions[direction].first;//第一组的第一个元素
int dc = directions[direction].second;//第一组的第二个元素

6.stack(后进先出)

cpp 复制代码
stack<int> s;
s.push(10);           // 压栈(O(1))
s.pop();              // 弹栈(O(1)),不返回元素
s.top();              // 查看栈顶元素(O(1))

queue

相关推荐
h39744 小时前
DirectShow过滤器开发-视频源过滤器
c++·windows·音视频·directshow
程序猿编码6 小时前
榨干 RTX5090 算力!Qwen3 专用单卡推理引擎,手写C++/CUDA 算子实现 MTP 推测解码
开发语言·c++·大模型推理·qwen3·多模态推理
西飘客7 小时前
vs 怎么根据dll 生成lib文件
c++·qt
程序员老陆8 小时前
深入理解 C++ thread_local:线程私有存储的正确打开方式
开发语言·c++·程序设计
信奥卷王8 小时前
[GESP202609 六级] 分树规划
算法
夏幻灵8 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
徐小黑ACG10 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
Navigator_Z10 小时前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode
北极有牛11 小时前
cpp学习笔记--常量指针
java·开发语言·算法