1: vector使用示例
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
// 初始化
vector<int> a;
vector<int> b(5); // 会初始化每个元素的值为0
vector<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个i
str4.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-value
m2.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); // 直接输入key
m2.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; // 创建一个空的map
map<string, int> m2(a.begin(), a.end()); // 使用迭代器进行复制,创建一个带有数据的map
map<string, int> m3(m2); // 拷贝
for (auto e:m1) {
// 由于是pair构造的对象,pair中first是第key, second是value
cout << " 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是value
cout << " 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中寻找某个key
auto it = f_counts.find("apple");//使用auto声明迭代器,找不到就返回end迭代器,找到就返回指向元素的迭代器
if (it != f_counts.end()) {
cout << "find successfully " << it->first << " " << it->second << endl; // 注意这里是->
}
// 删除某个key
it = 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: 常用方法
(1) 常用的函数
min();
max();
abs();
fabs();
pow(a, 2); // a ^ 2
sqrt(9); // 3 需要引用 #include <math.h>
INT_MAX // #include <climits> // 2^31 - 1
INT_MIN // #include <climits> // -2^31
sort(a.begin(), a.end(), cmp); // 自定义的比较函数,最好不要放在类里
reverse() // 这里可以是vector,也可以是string
(2) 寻找子串
string1.find("a") != string1.npos //找到了子串的位置 //该方法返回的是子串匹配的第一个字符
(3) 定义一个二维字符数组
vector<vector<char>> cc(3, vector<char>(4)>;
(4) map遍历,set遍历
unordered_map<string, tuple<int, int, int>> m;
//hash表赋值,[key]=value
m[str1] = make_tuple(1, 2, 3);
m[str2] = make_tuple(1, 2, 4);
//hash表遍历
for(auto i : m) {
string key = i.first; // 获取key
tuple<int, int, int> value = i.second; // 获取value
int a1 = get<0>(value);
int a2 = get<1>(value);
int a3 = get<2>(value);
}
//hash表通过key获取值,之后取tuple元素的具体值
int a1 = get<0>(m[str1]);
int a2 = get<1>(m[str1]);
int a3 = get<2>(m[str1]);
// 修改tuple的值,从而修改key对应的value
a1 += 1;
a2 += 2;
get<0>(m[str1]) = a1;
get<1>(m[str1]) = a2;
// 集合的遍历
set<int> s1;
s1.insert(1);
s1.insert(2);
for (auto i : s1) {
res.push_back(i);
}
(5)tuple 和 pair的常见用法
(6)
vector<unordered_map<string, int>> vec1; // vector存放vector是可以的,不过要注意的是修改方式,要通过数组[]的方式改变元素,而不是直接改表map
unordered_map<pair<int, int>, int> mp1; // 这种可能会报错,少用。
5: 排序
#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;
}