int t=a.find('x');//获取第一个字符x出现的位置的下标 string g=a.substr(0,3);//从下标0开始(包括0)往后取三个字符 stinrg g=a.substr(3)//从下标3开始到结束
algorithm
next_permutation,prev_permutation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//全排列函数 #include<bits/stdc++.h> usingnamespace std; intmain() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, a[100]; cin >> n; for (int i = 1; i <= n; i++) a[i] = i; do { for (int i = 1; i <= n; i++) cout << a[i] <<" "; cout << endl; } while (next_permutation(1 + a, 1 + a + n)); }
lower_bound
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//找大于等于某数的第一个数,查找的数组必须有序
int n = 7;//7个数 int a[] = { 2,4,6,7,9,12,111 }; int t = lower_bound(a, a + n, 8) - a;//查询范围:0 ~ 6。数组中大于等于8的第一个数
if (t != n)//找不到会返回边界a+n,即7 cout << a[t] << endl;
int b[] = { 0,2,4,6,7,9,12,111 }; t = lower_bound(b + 1, b + n + 1, 8) - b;//查询范围:1~8
if (t != n + 1)//找不到会返回边界,边界是 8 cout << b[t] << endl;
//如果是遍历删除 map 内特定的第 K 个数,跟 vector 删除一样要注意指针衔接 int k = 0; for (auto it = mp.begin(); it != mp.end();k++) { if (k == K)it = mp.erase(it); else it++; }//迭代器方式
int t = mp.count(3);//返回指定元素出现的次数 t = mp.size();//返回map中元素的个数
//查找 auto j = mp.find(3);//查找 3 关键字在 map 内的下标 //如果不存在则 j == mp.end() if (j != mp.end())cout << (*j).first;
auto g = mp.lower_bound(3);//查找 map 内第一个大于等于 3 的数的下标 //如果不存在则 g == mp.end() if (g != mp.end())cout << (*g).first;