摘要:基础部分、string、vector、queue、stack、set、deque、list…
string
输入输出字符串
1 2 3 4 5 6 7 8 9 10
| #include <bits/stdc++.h> using namespace std;
int main(){
string s; getline(cin, s); cout<<s; return 0; }
|
1 2 3
| 21314 hello woefsdcsd 21314 hello woefsdcsd Process finished with exit code 0
|
字符串拼凑
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <bits/stdc++.h> using namespace std;
int main(){
string s; s += "shda s"; s += "shda s"; s += '5';
cout<<s; return 0; }
|
1 2
| shda sshda s5 Process finished with exit code 0
|
排序
1 2 3 4 5 6 7 8 9 10
| #include <bits/stdc++.h>
using namespace std;
int main() { string s = "1523525431873"; sort(s.begin(), s.end()); cout << s; return 0; }
|
1 2
| 1122333455578 Process finished with exit code 0
|
s.begin(), s.end()
是迭代器,可以看成是指针
- 如果要访问最后一个字符,应该是
*(--s.end())
erase删除
1 2 3 4 5 6 7 8 9 10 11
| #include <bits/stdc++.h>
using namespace std;
int main() { string s = "1523525431873"; s.erase(s.begin()); s.erase(--s.end()); cout << s; return 0; }
|
1 2
| 52352543187 Process finished with exit code 0
|
substr取子串
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <bits/stdc++.h>
using namespace std;
int main() { string s = "1523525431873"; s = s.substr(2, 3); cout << s; s = "1523525431873"; s = s.substr(6, -1); cout << endl << s; return 0; }
|
1 2 3
| 235 5431873 Process finished with exit code 0
|
几种循环方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include <bits/stdc++.h>
using namespace std;
int main() { string s = "1523525431873"; for (int i = 0; i < s.length(); ++i) cout << s[i];
cout << '\n'; for (string::iterator iter = s.begin(); iter != s.end(); iter++) cout << *iter; cout << '\n'; for (auto i : s) cout << i;
cout << endl << s; return 0; }
|
1 2 3 4 5 6
| 1523525431873 1523525431873 1523525431873 1523525431873 Process finished with exit code 0
|
vector
初始化、打印
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <bits/stdc++.h>
using namespace std;
int main() { vector<int> v(3, 1); vector<int> v1(3); vector<int> v2{1, 2, 3, 4, 5}; for (auto x:v)cout << x; cout << '\n'; for (auto x:v1)cout << x; cout << '\n'; for (auto x:v2)cout << x; cout << '\n'; return 0; }
|
1 2 3 4 5
| 111 000 12345
Process finished with exit code 0
|
取元素 []或者at()
1 2 3 4 5 6 7 8 9 10 11
| #include <bits/stdc++.h>
using namespace std;
int main() { vector<int> v{1, 2, 3, 4, 5}; cout<<v[2]; cout<<v.at(4); return 0; }
|
追加push_back
1 2 3 4 5 6 7 8 9 10 11 12
| #include <bits/stdc++.h>
using namespace std;
int main() { vector<int> v{1, 2, 3, 4, 5}; v.push_back(7); v.push_back(7); v.push_back(7); for (auto x:v) cout << x; return 0; }
|
1 2
| 12345777 Process finished with exit code 0
|
resize重置大小,后面补0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <bits/stdc++.h>
using namespace std;
int main() { vector<int> v{1, 2, 3, 4, 5}; v.push_back(7); v.push_back(7); v.push_back(7); for (auto x:v) cout << x; v.resize(20); cout<<endl; for (auto x:v) cout << x; return 0; }
|
1 2 3
| 12345777 12345777000000000000 Process finished with exit code 0
|
erase删除,在vector中的复杂度为O(n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include <bits/stdc++.h>
using namespace std;
int main() { vector<int> v{1, 2, 3, 4, 5}; v.push_back(7); v.push_back(7); v.push_back(7); for (auto x:v) cout << x; cout<<endl; v.erase(v.begin()); v.erase(--v.end()); for (auto x:v) cout << x;
return 0; }
|
1 2 3
| 12345777 234577 Process finished with exit code 0
|
front(),back()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <bits/stdc++.h>
using namespace std;
int main() { vector<int> v{1, 2, 3, 4, 5}; v.push_back(7); v.push_back(7); v.push_back(7); for (auto x:v) cout << x; cout << endl; cout << v.front() << ' ' << v.back() << endl; cout << *v.begin() << ' ' << *(--v.end()) << ' ' << v[v.size() - 1]; return 0; }
|
1 2 3 4
| 12345777 1 7 1 7 7 Process finished with exit code 0
|
排序sort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <bits/stdc++.h>
using namespace std;
int main() { vector<int> v{1, 55, 15, 4, 25}; v.push_back(7); v.push_back(7); v.push_back(7); for (auto x:v) cout << x << ' '; cout << endl;
sort(v.begin(), v.end()); for (auto x:v) cout << x << ' '; cout << endl;
sort(v.rbegin(), v.rend()); for (auto x:v) cout << x << ' '; cout << endl;
return 0; }
|
1 2 3 4 5
| 1 55 15 4 25 7 7 7 1 4 7 7 7 15 25 55 55 25 15 7 7 7 4 1
Process finished with exit code 0
|
- 从大到小还可以用
sort(v.begin(), v.end(), greater<>());
循环与string的类似
- 用的较多的是
for (auto x:v) cout << x << ' ';
stack
初始化
- 一般刷算法题都直接这样写
stack<int> s;
- 如果不用
<bits/stdc++.h>
,那要用<stack>
1 2 3 4 5 6 7
| #include <bits/stdc++.h> using namespace std; int main() { stack<int> s; cout << endl; return 0; }
|
push, pop, top
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <bits/stdc++.h>
using namespace std;
int main() { stack<int> s; s.push(2); s.push(1); s.push(3); cout << s.top(); s.pop(); cout << s.top(); return 0; }
|
- push 入栈
- pop出栈
- top取栈顶
- size取长度
cout << s.empty();
是空就返回1,否则为0十进制转二进制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <bits/stdc++.h>
using namespace std;
int tenToTwo(int decimal) { stack<int> s; int res = 0; while (decimal != 0) { s.push(decimal % 2); decimal /= 2; } while (!s.empty()) { res = res * 10 + s.top(); s.pop(); } return res; }
int main() { cout << tenToTwo(11); return 0; }
|
1 2
| 1011 Process finished with exit code 0
|
逆序输出句子单词
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <bits/stdc++.h> using namespace std; int main() { stringstream ss; string str; getline(cin, str); ss << str; stack<string> s; while (ss >> str) { s.push(str); } while (!s.empty()) { cout << s.top(); if (s.size() != 1) cout << ' '; s.pop(); } return 0; }
|
1 2 3
| we are young man man young are we Process finished with exit code 0
|
字符串转化为数字
方法一
1 2 3 4 5 6 7 8 9 10 11 12
| #include <bits/stdc++.h> using namespace std; int main() { stringstream ss; string str = "12455"; ss << str; int num; ss >> num; cout << num; return 0; }
|
12455
Process finished with exit code 0
1 2 3 4 5 6 7 8 9 10
| ### 方法二 ```c++ int main() { stringstream ss; string str = "12455"; //把字符串转换为数字 int num = stoi(str); cout << num; return 0; }
|
1 2
| 12455 Process finished with exit code 0
|
数字转换为字符串
方法一
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <bits/stdc++.h> using namespace std;
int main() { stringstream ss; string str; int num = 12556; ss << num; ss >> str; cout << str; return 0; }
|
1 2
| 12556 Process finished with exit code 0
|
方法二
1 2 3 4 5 6 7 8 9 10 11 12
| #include <bits/stdc++.h> using namespace std;
int main() { stringstream ss; string str; int num = 12556; str = to_string(num); cout << str; return 0; }
|
queue
1 2 3 4 5 6 7 8 9 10 11
| #include <bits/stdc++.h> using namespace std;
int main() { queue<int> q; q.push(4); q.push(3); q.push(5); cout<<q.size()<<' '<<q.front(); return 0; }
|
map 和 unordered_map
- map: 有序的,底层是树状结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <bits/stdc++.h>
using namespace std;
int main() { map<int, int> dict; dict[1] = 2; dict[2] = 0; dict[4] = 12; for (auto item: dict) cout << item.first << ' ' << item.second << endl; for (auto iterator = dict.begin(); iterator != dict.end(); iterator++) cout << iterator->first << ' ' << iterator->second << endl; return 0; }
|
1 2 3 4 5 6 7 8
| 1 2 2 0 4 12 1 2 2 0 4 12
Process finished with exit code 0
|
- unordered——map:无序的,底层是哈希结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <bits/stdc++.h>
using namespace std;
int main() { unordered_map<int, int> dict; dict[1] = 2; dict[2] = 0; dict[4] = 12; for (auto item: dict) cout << item.first << ' ' << item.second << endl; for (auto iterator = dict.begin(); iterator != dict.end(); iterator++) cout << iterator->first << ' ' << iterator->second << endl; return 0; }
|
1 2 3 4 5 6 7 8
| 4 12 1 2 2 0 4 12 1 2 2 0 Process finished with exit code 0
|
- 打印出来是无序的
set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <bits/stdc++.h>
using namespace std;
int main() { unordered_set<int> s;
s.insert(2); s.insert(2); s.insert(3);
cout << s.size(); for (auto i:s) cout << i; return 0; }
|
1 2
| 232 Process finished with exit code 0
|
deque
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include <bits/stdc++.h> using namespace std;
int main() { deque<int> deque; deque.push_back(1); deque.push_back(2); deque.push_front(3); deque.push_front(4); for (auto i: deque) cout << i; deque.pop_back(); deque.pop_front(); cout<<endl; for (auto i: deque) cout << i; return 0; }
|
1 2 3
| 4312 31 Process finished with exit code 0
|
可以排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <bits/stdc++.h>
using namespace std;
int main() { deque<int> deque; deque.push_back(1); deque.push_back(2); deque.push_front(3); deque.push_front(4); for (auto i: deque) cout << i; sort(deque.begin(), deque.end()); cout<<endl; for (auto i: deque) cout << i; return 0; }
|
1 2 3
| 4312 1234 Process finished with exit code 0
|
list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include <bits/stdc++.h>
using namespace std;
int main() { list<int> list; list.push_front(2); list.push_front(3); list.push_back(4); list.push_back(1); for (auto i: list) cout << i << ' '; cout << endl; list.insert(++list.begin(), 99); for (auto i: list) cout << i << ' '; cout << endl; list.remove(3); for (auto i: list) cout << i << ' '; return 0; }
|
1 2 3 4
| 3 2 4 1 3 99 2 4 1 99 2 4 1 Process finished with exit code 0
|