C++滑动窗口算法模板
C++ 滑动窗口算法模板
滑动窗口是一种常用的算法技巧,主要用于解决数组/字符串的子区间问题。以下是
C++ 中滑动窗口的通用模板:
基本模板
int slidingWindowTemplate(vector<int>& nums, int k) { int n = nums.size(); int left = 0, right = 0; // 窗口左右边界 int result = 0; // 存储结果 unordered_map<int, int> window; // 用于记录窗口内元素的状态 while (right < n) { // 扩大窗口,加入右边元素 int c = nums[right]; right++; window[c]++; // 更新窗口状态 // 判断是否需要收缩窗口 while (window needs...
Pimpl模式
Pimpl(Pointer
to Implementation)模式简介
Pimpl(Pointer to Implementation),也称为
“Opaque Pointer”(不透明指针) 或 “Compilation
Firewall”(编译防火墙),是一种 C++
设计模式,用于隐藏类的实现细节,从而减少编译依赖、提高编译速度,并增强接口的稳定性。
1. Pimpl 的核心思想
将类的实现细节(私有成员)移动到一个单独的类(Impl)中
在公共接口类中仅保留一个指向实现类的指针
公共头文件(.h)仅包含声明,不暴露私有成员
传统类 vs. Pimpl 类
传统方式(暴露私有成员)
// Widget.h#include <string>#include <vector>class Widget {public: Widget(); void doSomething();private: std::string name; std::vector<int>...
CPP顺序型容器-list链表
std::list是 C++
标准库中的双向链表容器,支持高效的元素插入和删除操作。以下是
std::list的完整 API 参考。
1. 构造函数
方法
描述
时间复杂度
list<T> l
创建空 list
O(1)
list<T> l(count)
创建包含 count 个默认构造元素的
list
O(n)
list<T> l(count, value)
创建包含 count 个 value 的 list
O(n)
list<T> l(begin, end)
用迭代器范围初始化
O(n)
list<T> l(initializer_list)
用初始化列表初始化
O(n)
list<T> l(other_list)
拷贝构造函数
O(n)
list<T> l(move(other_list))
移动构造函数
O(1)
2....
C++基础知识
...
Redis基础知识
基础 + 数据结构
https://blog.csdn.net/fhkk55/article/details/149009568?ops_request_misc=&request_id=&biz_id=102&utm_term=Redis%E5%85%AB%E8%82%A1&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-2-149009568.142v102pc_search_result_base1&spm=1018.2226.3001.4187
为什么用 Redis 作为 MySQL 的缓存? Redis
之所以被广泛使用,主要因为它具备「高性能」和「高并发」两大核心特性。
Redis 具备高性能
当用户首次访问 MySQL
中的某些数据时,由于需要从硬盘读取,速度较慢。如果将这些数据缓存在 Redis
中,后续访问时就能直接从内存获取。由于...