[特殊字符] C++ 小白速通手册:常用方法 + 队列 / Vector / Map

[特殊字符] C++ 小白速通手册:常用方法 + 队列 / Vector / Map
一、C 常用基础方法速查1. 输入输出cin / cout#include iostream using namespace std; ​ int main() { int a; double b; string s; ​ // 基本输入 cin a b s; ​ // 输出 cout 整数: a endl; cout 小数: b endl; cout 字符串: s endl; ​ // 保留小数 cout fixed; // 固定小数位 cout.precision(2); // 保留2位 cout b endl; // 输出 3.14 ​ return 0; }2. 字符串string常用操作#include iostream #include string using namespace std; ​ int main() { string s Hello World; ​ // 长度 cout s.length() endl; // 11 ​ // 截取子串 string sub s.substr(0, 5); // Hello (从0开始取5个) ​ // 查找 size_t pos s.find(World); // 返回 6找不到返回 string::npos ​ // 替换 s.replace(6, 5, C); // Hello C ​ // 拼接 string a Hi, b Kim; string c a b; // Hi Kim ​ // 转数字 string num 123; int n stoi(num); // 123 string back to_string(n); // 123 ​ return 0; }3. 常用算法sort, reverse, max, min#include iostream #include algorithm // 算法头文件 #include vector using namespace std; ​ int main() { int arr[] {3, 1, 4, 1, 5, 9}; ​ // 排序升序 sort(arr, arr 6); // {1, 1, 3, 4, 5, 9} ​ // 排序降序 sort(arr, arr 6, greaterint()); // {9, 5, 4, 3, 1, 1} ​ // 反转 reverse(arr, arr 6); // {1, 1, 3, 4, 5, 9} 反转后 ​ // 最大最小值 int mx *max_element(arr, arr 6); int mn *min_element(arr, arr 6); ​ // 查找 bool found binary_search(arr, arr 6, 5); // true前提是已排序 ​ return 0; }4. 数学函数#include cmath #include cstdlib ​ abs(-5); // 整数绝对值 → 5 fabs(-3.14); // 浮点绝对值 → 3.14 sqrt(16); // 开平方 → 4 pow(2, 10); // 幂运算 → 1024 ceil(3.2); // 向上取整 → 4 floor(3.9); // 向下取整 → 3 round(3.5); // 四舍五入 → 4二、队列Queue—— 排队买票核心思想先进先出FIFO就像排队买票先排的先走。#include iostream #include queue // 队列头文件 using namespace std; ​ int main() { queuestring q; // 创建一个存字符串的队列 ​ // 入队排队 q.push(张三); // 队尾加入 q.push(李四); q.push(王五); ​ // 查看队首队尾 cout 队首: q.front() endl; // 张三 cout 队尾: q.back() endl; // 王五 cout 人数: q.size() endl; // 3 ​ // 出队处理完走人 while (!q.empty()) { // 只要队列不为空 cout q.front() 办理完毕 endl; q.pop(); // 队首出队删除 } // 输出顺序张三 → 李四 → 王五 ​ return 0; } Queue 常用方法速查表方法作用示例push(x)队尾加入元素q.push(10)pop()删除队首元素q.pop()front()查看队首元素int x q.front()back()查看队尾元素int x q.back()empty()判断是否为空if (q.empty())size()返回元素个数int n q.size()⚠️注意pop()只删除不返回值想取值要先front()再pop()。三、Vector —— 动态数组超级常用核心思想一个可以自动扩容的数组不用提前定死大小。#include iostream #include vector // vector头文件 using namespace std; ​ int main() { // 创建方式 vectorint v1; // 空数组 vectorint v2(5); // 5个元素默认值为0 vectorint v3(5, 100); // 5个元素每个都是100 vectorint v4 {1, 2, 3, 4, 5}; // 直接初始化 ​ // 添加元素 v1.push_back(10); // 尾部添加 v1.push_back(20); v1.push_back(30); // v1 {10, 20, 30} ​ v1.pop_back(); // 删除尾部v1 {10, 20} ​ // 访问元素 cout v1[0] endl; // 10 不检查越界快但不安全 cout v1.at(0) endl; // 10 检查越界安全但稍慢 cout v1.front() endl; // 10 第一个 cout v1.back() endl; // 20 最后一个 ​ // 插入和删除 vectorint v {1, 2, 3, 4, 5}; ​ // 在位置2下标2插入99 v.insert(v.begin() 2, 99); // {1, 2, 99, 3, 4, 5} ​ // 删除位置2的元素 v.erase(v.begin() 2); // {1, 2, 3, 4, 5} ​ // 清空 v.clear(); // 变成空数组 ​ // 遍历方式3种 vectorint nums {10, 20, 30, 40}; ​ // 方式1下标遍历最常用 for (int i 0; i nums.size(); i) { cout nums[i] ; } ​ // 方式2迭代器遍历 for (auto it nums.begin(); it ! nums.end(); it) { cout *it ; } ​ // 方式3范围for循环最简洁推荐 for (int x : nums) { cout x ; } ​ // 排序 查找 vectorint a {3, 1, 4, 1, 5}; ​ sort(a.begin(), a.end()); // 升序排序 sort(a.begin(), a.end(), greaterint()); // 降序 ​ // 二分查找必须先排序 bool found binary_search(a.begin(), a.end(), 4); ​ // 找某个值的位置 auto it find(a.begin(), a.end(), 4); if (it ! a.end()) { cout 找到了位置 (it - a.begin()) endl; } ​ return 0; } Vector 常用方法速查表方法作用push_back(x)尾部添加pop_back()尾部删除insert(pos, x)在位置pos插入xerase(pos)删除位置pos的元素clear()清空size()元素个数empty()是否为空resize(n)改变大小为nbegin()/end()返回开头/结尾迭代器四、Map —— 字典/键值对超级好用核心思想像查字典一样给一个键key找到对应的值value。#include iostream #include map // map头文件 #include string using namespace std; ​ int main() { // 创建 mapstring, int score; // key是stringvalue是int ​ // 添加/修改元素 score[张三] 90; score[李四] 85; score[王五] 92; ​ // 或者这样插入 score.insert({赵六, 88}); ​ // 访问元素 cout 张三的分数: score[张三] endl; // 90 ​ // ⚠️ 注意用 [] 访问不存在的key会自动创建 // 如果只想查不想创建用 find auto it score.find(不存在的人); if (it score.end()) { cout 查无此人 endl; } else { cout 分数: it-second endl; } ​ // 遍历自动按key排序 cout \n 成绩单 endl; for (auto pair : score) { // pair.first 是keypair.second 是value cout pair.first : pair.second 分 endl; } // 输出会按名字拼音/字母顺序排序 ​ // 判断是否存在 if (score.count(张三)) { cout 张三在表里 endl; } ​ // 删除 score.erase(李四); // 删除李四的记录 ​ // 大小 cout 当前人数: score.size() endl; ​ // 实用场景统计单词出现次数 string text apple banana apple orange banana apple; mapstring, int wordCount; ​ // 假设我们已经分好词了 vectorstring words {apple, banana, apple, orange, banana, apple}; ​ for (string word : words) { wordCount[word]; // 出现一次就1超级方便 } ​ cout \n 单词统计 endl; for (auto p : wordCount) { cout p.first 出现了 p.second 次 endl; } // apple: 3, banana: 2, orange: 1 ​ return 0; } Map 常用方法速查表方法作用m[key] value添加/修改m.insert({key, val})插入key已存在则不插入m.find(key)查找返回迭代器m.count(key)判断是否存在返回0或1m.erase(key)删除m.size()元素个数m.empty()是否为空m.clear()清空Map 会自动按 key 排序默认升序如果你不想排序用unordered_map用法完全一样只是遍历顺序不固定。五、综合实战学生成绩管理系统把上面学的串起来#include iostream #include vector #include map #include queue #include algorithm using namespace std; ​ struct Student { string name; int score; }; ​ int main() { // 用vector存所有学生 vectorStudent students; ​ // 用map快速查分数 mapstring, int scoreMap; ​ // 用queue模拟交作业顺序 queuestring homeworkQueue; ​ // 添加学生 students.push_back({张三, 85}); students.push_back({李四, 92}); students.push_back({王五, 78}); ​ // 填入map和队列 for (auto s : students) { scoreMap[s.name] s.score; homeworkQueue.push(s.name); } ​ // 按分数排序用lambda表达式 sort(students.begin(), students.end(), [](Student a, Student b) { return a.score b.score; // 分数高的在前 }); ​ cout 成绩排名 endl; for (auto s : students) { cout s.name : s.score endl; } ​ cout \n 查分 endl; cout 李四的分数: scoreMap[李四] endl; ​ cout \n 作业提交顺序 endl; while (!homeworkQueue.empty()) { cout homeworkQueue.front() 提交了作业 endl; homeworkQueue.pop(); } ​ return 0; }️ 推荐在线编译器Online C Compiler - Programiz不用安装打开就能写