【C++算法竞赛函数速查表】
- IT业界
- 2025-09-03 01:00:01

C++ 算法竞赛函数速查表 常用头文件 <iostream>:输入输出流<vector>:动态数组<algorithm>:常用算法<set>:集合<map>:映射<unordered_map>:哈希映射<queue>:队列<deque>:双端队列<stack>:栈<string>:字符串处理<cmath>:数学函数<limits>:获取数值类型的极限值<utility>:实用工具如pair<tuple>:元组<functional>:函数对象<numeric>:数值算法<bitset>:位集合<random>:随机数生成 STL 数据结构 动态数组 - std::vector 创建:std::vector<int> v;初始化:std::vector<int> v = {1, 2, 3};访问元素:v[i]添加元素:v.push_back(x);删除末尾元素:v.pop_back();获取大小:v.size();清空:v.clear();预分配空间:v.reserve(size); 集合 - std::set 创建:std::set<int> s;插入元素:s.insert(x);删除元素:s.erase(x);检查是否存在:s.find(x) != s.end()获取第一个元素:*s.begin();获取最后一个元素:*s.rbegin(); 映射 - std::map 创建:std::map<std::string, int> m;插入/更新键值对:m["key"] = value;查找键值对:if (m.count("key")) { /* 存在 */ }删除键值对:m.erase("key");获取第一个键值对:auto it = m.begin(); std::cout << it->first << " " << it->second; 哈希映射 - std::unordered_map 创建:std::unordered_map<std::string, int> um;插入/更新键值对:um["key"] = value;查找键值对:if (um.count("key")) { /* 存在 */ }删除键值对:um.erase("key"); 队列 - std::queue 创建:std::queue<int> q;入队:q.push(x);出队:q.pop();获取队首元素:q.front();获取队尾元素:q.back();判断是否为空:q.empty(); 双端队列 - std::deque 创建:std::deque<int> dq;访问元素:dq[i]在前端添加元素:dq.push_front(x);在后端添加元素:dq.push_back(x);在前端删除元素:dq.pop_front();在后端删除元素:dq.pop_back(); 栈 - std::stack 创建:std::stack<int> stk;入栈:stk.push(x);出栈:stk.pop();获取栈顶元素:stk.top();判断是否为空:stk.empty(); 字符串 - std::string 创建:std::string str = "hello";连接:str += " world";截取子串:str.substr(start, length);转换为整数:std::stoi(str);转换为浮点数:std::stod(str); 常用算法 排序 升序排序:std::sort(v.begin(), v.end());自定义比较函数:std::sort(v.begin(), v.end(), [](int a, int b) { return a > b; });部分排序:std::partial_sort(v.begin(), v.begin() + k, v.end()); 查找 二分查找:std::binary_search(v.begin(), v.end(), x);最小值:*std::min_element(v.begin(), v.end());最大值:*std::max_element(v.begin(), v.end());计数:std::count(v.begin(), v.end(), x); 数学运算 绝对值:std::abs(x);幂运算:std::pow(base, exponent);四舍五入:std::round(x);最大公约数:std::gcd(a, b);最小公倍数:std::lcm(a, b); 数值算法 积累求和:std::accumulate(v.begin(), v.end(), 0);内积:std::inner_product(v1.begin(), v1.end(), v2.begin(), 0);相邻差:std::adjacent_difference(v.begin(), v.end(), result.begin());前缀和:std::partial_sum(v.begin(), v.end(), result.begin()); 随机数 创建随机数引擎:std::default_random_engine generator;生成随机整数:std::uniform_int_distribution<int> distribution(min, max); int dice_roll = distribution(generator); 其他 交换两个变量:std::swap(a, b);时间测量:std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
【C++算法竞赛函数速查表】由讯客互联IT业界栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“【C++算法竞赛函数速查表】”