cpp数据结构

发布时间:2026/7/5 15:26:28
cpp数据结构 删除方法vectorint v {1,2,3,4,5}; 1、删除一个元素 v.erase(v.begin()2); // 删除3 2、删除一段 v.erase(v.begin()1, v.begin()4); //删除2 3 4 3、删除最后一个 v.pop_back(); 4、删除所有 v.clear(); 6、按条件删除 v.erase(remove_if(v.begin(),v.end(), [](int x){ return x%20; }),v.end()); string 字符串本质类似于 vectorchar string sabcdef; s.erase(2,1); // abdef 删除后半段 s.erase(3); abcset / multiset setint s{1,2,3,4}; 删除值 s.erase(2); // 结果1 3 4 删除迭代器 auto its.find(3); s.erase(it); 删除区间 s.erase(s.begin(),s.find(4)); map / multimap mapstring,int mp; 删除键 mp.erase(Tom); 删除迭代器 mp.erase(mp.begin()); 删除范围 mp.erase(it1,it2);复合类型pair p {a,10}; p.first p.second tuple t {a,10,20}; get0(t) get1(t) get2(t) structs {a,10,20}; s.name s.hot s.pricetuple头文件tuple可以存N 个值访问用get下标()下标从 0 开始按“索引”访问#include tuple #include iostream using namespace std; int main() { tupleint, double, string t {1, 3.14, hello}; cout get0(t) endl; // 1 cout get1(t) endl; // 3.14 cout get2(t) endl; // hello }按“类型”访问有条件 限制类型必须唯一tupleint, double, string t {1, 3.14, hi}; cout getint(t) endl; // 1 cout getdouble(t) endl; // 3.14访问 tuple 大小tupleint, double, string t; cout tuple_sizedecltype(t)::value endl; // 3pair头文件utility只能存2 个值 访问用.first .second#include iostream #include utility // pair using namespace std; int main() { pairstring, int p {apple, 10}; // 访问方式 cout p.first endl; // apple cout p.second endl; // 10 }struct 访问自己定义成员名 访问用.自定义名字#include iostream #include string using namespace std; // 定义结构体 struct Item { string name; int hot; int price; }; int main() { Item item {apple, 10, 100}; // 访问方式 cout item.name endl; // apple cout item.hot endl; // 10 cout item.price endl; // 100 }排序pair 排序 自带字典序自动比较从左到右逐元素比sort(v.begin(), v.end()); // 先比 first再比 secondtuple 排序 自带字典序自动比较从左到右逐元素比sort(v.begin(), v.end()); // 自动字典序get0 → get1 → get2struct 排序 必须自己写规则sort(v.begin(), v.end(), [](const Item a, const Item b) { return a.name b.name; });二、顺序容器线性数组类1.array静态数组头文件array固定长度、栈上分配arrayint, 5 arr {1,2,3,4,5};访问接口arr[i] 下标访问 .front() 首元素 .back() 尾元素 .size() 元素个数2.vector动态数组自动扩容连续内存vectorstring v{app, apple, banana};访问接口v[i] 下标随机访问 .front() 第一个 .back() 最后一个 .size() 大小 .begin() 起始迭代器 .end() 末尾迭代器3.string字符串本质字符容器头文件string访问接口s[i] 按下标取字符 .size()/.length() 长度 支持字典序直接 比较4.list双向链表头文件list不支持下标[]只能迭代器遍历访问只能用迭代器for (auto x : lst)5. string访问接口一、基础信息接口最常用 1. size() / length()完全等价 2. empty() 3. s.clear(); // 变成 二、访问字符接口 1. operator[] 不检查越界危险⚠️ 2. at()安全版会检查越界,越界抛异常 3. front() / back() string s hello; cout s.front(); // h cout s.back(); // o 三、修改字符串 s[0] H; s.append( world); 等价于 s world; s.push_back(!); s.pop_back(); 删除最后一个字符 四、查找接口非常重要⭐ 1. find() 返回 找到位置 找不到string::npos 2. rfind() 从右往左找 3. find_first_of() 找任意字符 4. find_last_of() 最后出现的字符位置 五、截取子串核心⭐ substr() string s hello world; string t s.substr(6, 5); // t world 八、转换接口高频⭐ 1. string - int stoi(123); 2. string - long long stoll(123456789); 3. int - string to_string(123);三、关联容器键值 / 有序查找容器头文件存储结构是否有序键是否重复访问 / 取值查找速度底层实现mapK,Vmap键值对 key-value升序有序键唯一不重复m[key]、迭代器O(logn)红黑树unordered_mapK,Vunordered_map键值对 key-value无序键唯一不重复m[key]、迭代器平均 O (1)哈希表setKset只存 key升序有序元素唯一去重迭代器遍历O(logn)红黑树unordered_setKunordered_set只存 key无序元素唯一去重迭代器遍历平均 O (1)哈希表mapmap取值[]如果 key 不存在自动插入 keyvalue 用默认值初始化at()key 不存在会抛异常find() *************取值之前最好先find一下返回迭代器auto it mp.find(apple); // 找到 if (it ! mp.end()) { cout it-second; } //没找到 if (it mp.end())count()if (mp.count(apple))map 中存在返回 1 不存在返回 0 因为 map 不允许重复 key。contains()C20if (mp.contains(apple)) 等价于 if (mp.find(apple) ! mp.end())map遍历方法int main() { mapstring, int mp; mp[apple] 3; mp[banana] 5; mp[orange] 2; }1. ******迭代器遍历最经典for (mapstring, int::iterator it mp.begin(); it ! mp.end(); it) { cout it-first it-second endl; }2. auto 简化实际开发最常用for (auto it mp.begin(); it ! mp.end(); it) { cout it-first it-second endl; }3.********** 范围 for 遍历C11方法1直接取 pairfor (auto p : mp) { cout p.first p.second endl; }方法2引用推荐for (auto p : mp) { cout p.first p.second endl; } for (pairconst string, int p : mp) { cout p.first p.second endl; }4. 结构化绑定C17非常高级for (auto [key, value] : mp) { cout key value endl; }5. 遍历时删除元素重点面试//错误写法 for (auto it mp.begin(); it ! mp.end(); it) { if (it-second 5) mp.erase(it); // 错误 } // 因为 erase 后迭代器失效。 //正确写法 for (auto it mp.begin(); it ! mp.end(); ) { if (it-second 5) it mp.erase(it); // // 返回下一个有效迭代器 else it; }map常用容量接口mp.size() 元素数量。 mp.empty() 是否为空。 mp.clear() 清空。 map插入元素 mp.insert(pairstring, int(apple, 10)); mp.insert(make_pair(banana, 20)); mp.insert({orange, 30}); auto result mp.insert({apple, 100}); cout *result .first endl; 输出apple 这个result 类型为pairiterator, bool 12. 哈希表特有接口高级 mp.bucket_count() 桶数量 rehash() 重新分桶 mp.load_factor() 负载因子 reserve() 预留空间setset特有接口s.lower_bound(5) 第一个 5 的元素 s.upper_bound(5) 第一个 5 的元素 auto p s.equal_range(5); 返回[lower_bound, upper_bound)set遍历setint s {1, 3, 5, 7}; for (setint::iterator it s.begin(); it ! s.end(); it) { cout *it endl; }