C++常用数据结构

news/2024/10/7 15:09:33/

1: vector使用示例

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;int main()
{// 初始化vector<int> a;vector<int> b(5); // 会初始化每个元素的值为0vector<int> c(6, 2);vector<int> d = {1, 3, 45};vector<int> e(d);vector<int> f(c);vector<int> g(b);// 插入a.push_back(100);b.push_back(100);c.push_back(100);d.push_back(100);f.push_back(100);// 遍历for(int i = 0; i < e.size(); i++) {cout << "e " << i << "is " << e[i] << endl;}for(auto i:f) {cout << "f " << i << endl;}// 排序 翻转  vector<int> vec(3, -1);vec.push_back(100);vec[0] = 1;int len = vec.size();vec[len - 1] = 1000;sort(vec.begin(), vec.end());reverse(vec.begin(), vec.end());int first_ = vec.front();int end_ = vec.back();cout << "cur vec size is " << vec.size() << " and " <<  first_ << " " << end_  << endl;// 插入删除vector<int>::iterator it;it = vec.begin();vec.insert(it + 2, 99000);vec.insert(it, 2, 100);cout << "cur vec size is " << vec.size() << " and " <<  first_ << " " << end_  << endl;vec.erase(it+2);cout << "cur vec size is " << vec.size() << " and " <<  first_ << " " << end_  << endl;vec.pop_back();cout << "cur vec size is " << vec.size() << " and " <<  first_ << " " << end_  << endl;// 二维数组vector<vector<int>> aa;aa.resize(6);   for (int i = 0; i < aa.size(); i++) {aa[i].resize(7);}vector<vector<int>> bb(6, vector<int>(5));vector<int> bb1(5, 1);vector<int> bb2(5, 2);vector<int> bb3(5, 3);bb.push_back(bb1);bb.push_back(bb2);bb.push_back(bb3);cout << "print bb" << endl;for (int i = 0; i < bb.size(); i++) {for(auto j: bb[i]) {cout << j << endl;} }cout << "hello world" << endl;
}

​​​​​​​2: string示例

#include <iostream>
#include <string>using namespace std;int main()
{// string 初始化string str1;string str2(" I love you "); // 空格字符也是有效的string str3(3, 'd');string str4(str2);cout << str4 << endl;cout << str3 << endl;// 字符串有效长度int str_size = str1.size();int str_len = str1.length();int str2_len = str2.length();int str2_size = str2.size();cout << "str1 size is "  << str_size << " and length is " << str_len << endl;cout << "str2 size is " << str2_size << " and length is " << str2_len << endl;// 字符串判空bool is_empty = str1.empty();cout << "str1 is empty ? : " << is_empty << endl;// resize操作str3.resize(2); // 修改字符串长度,要么截断(截断的多余部分用'\0', 扩展时候若没有指定对应的元素则会补充'\0')cout << "str3 is " << str3 << endl;str3.clear(); // 字符串清零,变为空串str3.resize(5); // 扩展大小为5, 元素都是'\0'cout << "str3 size is " << str3.size() << " " << str3 << endl;str3 += "good"; // cout << "str3 size is " << str3.length() << " " <<  str3.size() << " " << str3 << endl;str3[0] = 'a';str3[2] = 'b';str3[3] = 'c';cout << "str3 is " << endl;cout << "str3 size is " << str3.length() << " " <<  str3.size() << " " << str3 << endl;// 返回第一个字符,和最后一个字符, []查找char fist_c = str3.front();char back_c = str3.back();char c = str3[2];cout << "the first char and last char is " << fist_c << " " << back_c << endl;cout << "the char is " << c << endl;// 字符串的插入和删除str4 = "asd"; // 大小和值都重新覆盖cout << "str4 is " << str4 << " and size is " << str4.size() << endl;str4 += "e";str4.push_back('f'); // 末尾追加str4.append("gh"); // 末尾追加str4.append(3, 'i'); // 末尾追加3个istr4.pop_back(); // 弹出末尾元素str4.insert(2, "asd");str4.erase(0, 1);//从第0个位置删除1个元素cout << "str4 is " << str4 << " and size is " << str4.size() << endl;// 字符串常用操作方法string s = "hello";string s_s = s.substr(1, 3); // 从1开始之后的3个元素cout << "substr is " << s_s << endl;// 字符串查找string s1("I have a dream");int first_pos = s1.find("d"); // 第一次出现的位置int last_pos = s1.rfind("a"); // 最后一次出现的位置cout << first_pos << " and  " << last_pos << endl;// 字符串替换s1.replace(1, 2, "ss"); // 从索引1开始替换,替换2个元素,用sss替换// 字符串比较string s2 = "ssa";string s3 = "ssb";int ret = s2.compare(s3);if (ret == 0) {cout << " s2 = s3" << endl;} else if (ret > 0) {cout << " s2 > s3 " << endl;} else if (ret < 0) {cout << " s2 < s3 " << endl;}// 字符转化为数字,或数字转化为字符string num = "123";int n = stoi(num);cout << "num is " << n << endl;string num2 = to_string(124);cout << num2 << endl;return 0;
}

3: map & set 示例

#include <iostream>
#include <vector>
#include <algorithm>
#include <map> // 使用map需要包括这个头文件
#include <set>
#include <string>using namespace std;/*map 中的元素是键值对map 中的 key 是唯一的,并且不能修改默认按照小于的方式对 key 进行比较map 中的元素如果用迭代器去遍历,可以得到一个有序的序列map 的底层为平衡搜索树(红黑树),查找效率比较高 O(logN)支持 [ ] 操作符,operator[ ] 中实际进行插入查找unordered_map 是不保证顺序的,这样他的查找理论上是要比map要快的
*/// map 是关联式容器,在map中元素总是按照key值进行排序的。O(logn), map支持【key】进行元素键值对访问void use_set_and_map()
{map<int, int> m1;map<int, pair<int, int>> m2;map<int, tuple<int, int, int>> m3;m1.insert({1, 1}); // 直接插入key-valuem2.insert({1, make_pair(4, 2)});m3.insert({1, make_tuple(4, 2, 3)});m1.insert({2, 1});m2.insert({2, make_pair(4, 2)});m3.insert({2, make_tuple(4, 2, 3)});if (m1.find(1) != m1.end())  {cout << "find 1 in m1" << endl;}if (m2.find(1) != m2.end() && m2[1].first == 4) {cout << "find 1 in m2" << endl;}if (m3.find(1) != m3.end() && get<0>(m3[1]) == 4) {cout << "find 1 in m3" << endl;}cout << "m1 size is " << m1.size() << " and m2 size is " << m2.size()  << " and m3 size is " << m3.size() << endl;m1.erase(1); // 直接输入keym2.erase(1);m3.erase(1);cout << "m1 size is " << m1.size() << " and m2 size is " << m2.size()  << " and m3 size is " << m3.size() << endl;if (m3.find(1) != m3.end() && get<0>(m3[1]) == 4) {cout << "find 1 in m3" << endl;} else {cout << "not found m3-key-1" << endl;}return;
}int main()
{cout << " start test map " << endl;vector<pair<string, int>> a = {make_pair("a", 123), make_pair("H", 1234), make_pair("s", 1)}; // make_pair构造数据map<string, int> m1; // 创建一个空的mapmap<string, int> m2(a.begin(), a.end()); // 使用迭代器进行复制,创建一个带有数据的mapmap<string, int> m3(m2); // 拷贝for (auto e:m1) {// 由于是pair构造的对象,pair中first是第key, second是valuecout << " m1 " << e.first << " and " << e.second << endl;}for (auto e:m2) {cout << " m2 " << e.first << " and " << e.second << endl;}// 插入操作,使用insert方法m1.insert(pair<string, int>("u", 99));m1.insert(pair<string, int>("i", 100));m2.insert(pair<string, int>("o", 1));m1.insert(make_pair("d", 8)); // 推荐make_pair方式创建键值对m2.insert(make_pair("e", 31));for (auto e:m1) {// 由于是pair构造的对象,pair中first是第key, second是valuecout << " cur m1 " << e.first << " and " << e.second << endl;}for (auto e:m2) {cout << " cur m2 " << e.first << " and " << e.second << endl;}//  插入元素到map,使用下标【】, 注意不支持【】随机访问vector<string> fronts = {"apple", "banana", "watermelon", "apple"};map<string, int> f_counts;for (auto it:fronts) {f_counts[it]++; // map元素插入,这里保证不会出现重复的key}for (auto it:f_counts) {cout << it.first << " num is " << it.second << endl;}// 在map中寻找某个keyauto it = f_counts.find("apple");//使用auto声明迭代器,找不到就返回end迭代器,找到就返回指向元素的迭代器if (it != f_counts.end()) { cout << "find successfully " <<  it->first << " " << it->second << endl; // 注意这里是->}// 删除某个keyit = f_counts.find("watermelon"); // 查找if (it != f_counts.end()) {f_counts.erase(it); // 删除当前key}// map常用方法  bool is_empty = f_counts.empty(); // 判断是否为空int f_size = f_counts.size(); // map的大小int f_num1 = f_counts.count("apple"); // 元素出现的次数 (由于map的key是唯一的,所以次数只能是1,0代表没有这个key)int f_num2 = f_counts.count("banana"); // 元素出现的次数while (1) { cout << " size is " << f_size << endl; cout << " empty ? " << is_empty << endl;cout << " apple nums is " << f_num1 << endl;cout << " banana nums is " << f_num2 << endl;break;}use_set_and_map();return 0;
}

4: 排序

#include <iostream>
#include <vector>
#include <algorithm>using namespace std;// 自定义比较函数,输入是一个vector
const bool cmp(const vector<int>& va, const vector<int>& vb)
{if (va[0] == vb[0]) {return va[1] > vb[1]; // 降序}return va[0] < vb[0]; // 升序
}int main()
{// 初始化vector<int> a;a.push_back(100);a.insert(a.begin(), 109);a.insert(a.begin() + 1, 2, 200);a.push_back(9);// 排序sort(a.begin(), a.end());for(auto i:a) {cout << i << endl;}// 二维数组的排序vector<vector<int>> aa;aa.push_back({1,  2});aa.push_back({2, 1});aa.push_back({1, 3});aa.push_back({3, 3});aa.push_back({4, 1});aa.push_back({4, 2});sort(aa.begin(), aa.end(), cmp);for(int i = 0; i < aa.size(); i++) {for(int j = 0; j < aa[0].size(); j++) {cout << aa[i][j] << " ";}cout << endl;}return 0;
}

http://www.ppmy.cn/news/1535748.html

相关文章

yolov11 部署瑞芯微rk3588、RKNN部署工程难度小、模型推理速度快

yolov8还没玩溜&#xff0c;yolov11又来了&#xff0c;那么部署也又来了。 特别说明&#xff1a;如有侵权告知删除&#xff0c;谢谢。 完整代码&#xff1a;包括onnx转rknn和测试代码、rknn板端部署C代码 【onnx转rknn和测试代码】 【rknn板端部署C代码】 1 模型训练 yolov1…

Acwing 数位统计DP

Acwing 338.计数问题 输入样例&#xff1a; 1 10 44 497 346 542 1199 1748 1496 1403 1004 503 1714 190 1317 854 1976 494 1001 1960 0 0 输出样例&#xff1a; 1 2 1 1 1 1 1 1 1 1 85 185 185 185 190 96 96 96 95 93 40 40 40 93 136 82 40 40 40 40 115 666 215 215 214…

系统设计,如何设计一个秒杀功能

需要解决的问题 瞬时流量的承接防止超卖预防黑产避免对正常服务的影响兜底方法 前端设计 利用 CDN 缓存静态资源&#xff0c;减轻服务器的压力在前端随机限流按钮防抖&#xff0c;防止用户重复点击 后端设计 Nginx 做统一接入&#xff0c;进行负载均衡与限流用 sentinel 等…

Android15车载音频之Virtualbox中QACT实时调试(八十八)

简介: CSDN博客专家、《Android系统多媒体进阶实战》一书作者 新书发布:《Android系统多媒体进阶实战》🚀 优质专栏: Audio工程师进阶系列【原创干货持续更新中……】🚀 优质专栏: 多媒体系统工程师系列【原创干货持续更新中……】🚀 优质视频课程:AAOS车载系统+…

设计模式之装饰器模式(Decorator)

一、装饰器模式介绍 装饰模式(decorator pattern) 的原始定义是&#xff1a;动态的给一个对象添加一些额外的职责。 就扩展功能而言&#xff0c;装饰器模式提供了一种比使用子类更加灵活的替代方案。 在软件设计中&#xff0c;装饰器模式是一种用于替代继承的技术&#xff0c;它…

【信息系统项目管理师考题预测】合同管理

信息系统项目合同管理是项目管理中的重要环节,其常考题目通常涉及合同管理的各个方面,包括合同的订立、履行、变更、终止以及违约索赔管理等。 一、选择题 以下哪项不属于合同管理的范畴? A. 合同的订立 B. 回答潜在卖方的问题 C. 合同的履行 D. 合同的变更 解析:B。回答潜…

C++ list 容器类的模拟实现

前言&#xff1a; 我们不仅仅要熟悉使用c标准库中的 list 容器&#xff0c;我们更要学习了解标准库是如何将 list 容器实现出来的&#xff0c;这就是我们常说的知其然也要知其所以然&#xff0c;学习源码中优秀的部分&#xff0c;将 list 容器进行模拟实现出来&#xff0c;使得…

C++ 泛型编程指南 类模板,类型推断,特化,别名模板

文章目录 [TOC]1.声明一个类模板2.模板类成员函数实现3. 使用 Stack 类模板 4.部分使用类模板5.模板类的特例化6. 模板类的偏特化6.1 区分偏特化和全特化6.1.1 偏特化6.1.2 全特化 6.2 偏特化 例子6.2.1 指针类型偏特化6.2.2 多个参数的偏特化 解释&#xff1a;6.3 偏特化匹配歧…