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;
}