[toc]
头文件
1 2 3
| #include <ciostream> #include <string> #include <cctype>
|
cout 输出控制
1 2 3 4 5 6 7 8 9 10 11
| #include <iostream> #include <string> using namespace std; int main() { bool t = true; cout << t << endl; cout << boolalpha << t << endl; return 0; }
|
C++定义二维数组
1
| vector<vector<int>> res(n, vector<int>(n, 0));
|
字符串
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 26 27 28 29 30 31 32 33 34 35 36 37 38
| #include <iostream> #include <string> using namespace std; int main() { string s1 = "hello", s2 = " world"; cout << s1 + s2 << endl; string s, t; cin >> s; getline(cin, t); int length = s.length(); s = "aaabbb", t1 = "abb", t2 = "c"; s.find(t1); s.find(t2); s = "hello world"; reverse(s.begin(), s.end()); cout << s << endl; s = "hello world"; cout << s.substr(2, 3) << endl; return 0; }
|
栈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include <iostream> #include <stack> using namespace std; int main() {
stack<int> s; cout << boolalpha << s.empty() << endl; s.push(8); cout << s.top() << endl; cout << s.size() << endl; s.pop(); return 0; }
|
队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <iostream> #include <queue> int main() {
return 0; }
|
集合
set中各元素各不相同,且 set 内升序排序
unordered_set 即是官方的 hash_set
集合 |
底层实现 |
是否有序 |
数值是否可重复 |
能否更改数值 |
查询效率 |
增删效率 |
std::set |
红黑树 |
有序 |
否 |
否 |
O(log n) |
O(log n) |
std::multiset |
红黑树 |
有序 |
是 |
否 |
O(log n) |
O(log n) |
std::unordered_set |
哈希表 |
无序 |
否 |
否 |
O(1) |
O(1) |
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 26 27 28 29 30 31
|
#include <iostream> #include <set> using namespace std; int main() {
set<int> s; s.insert(2); s.insert(3); cout << *(s.begin()) << endl; for (int i = 0; i < 10; i++) { s.insert(i); } for (set<int>::iterator it = s.begin(); it != s.end(); it++) { cout << *it << " "; } cout << endl << (s.find(2) != s.end()) << endl; s.erase(3); cout << (s.find(3) != s.end()) << endl; return 0; }
|
map
映射 |
底层实现 |
是否有序 |
数值是否可以重复 |
能否更改数值 |
查询效率 |
增删效率 |
std::map |
红黑树 |
key有序 |
key不可重复 |
key不可修改 |
O(log n) |
O(log n) |
std::multimap |
红黑树 |
key有序 |
key可重复 |
key不可修改 |
O(log n) |
O(log n) |
std::unordered_map |
哈希表 |
key无序 |
key不可重复 |
key不可修改 |
O(1) |
O(1) |
unordered_map 即是官方的 hash_map
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 26 27 28 29 30 31 32 33 34 35 36 37 38
| #include <iostream> #include <unordered_map> #include <string> using namespace std; int main() {
map<string, string> m; m["hello"] = "world"; cout << m["hello"] << endl; cout << m["world"] << endl; m["world"] = "test"; m["key"] = "value"; for (map<string, string>::iterator it = m.begin(); it != m.end(); it++) { cout << it->first << " " << it->second << endl; }
cout << m.begin()->first << " " << m.begin()->second << endl;
cout << m.rbegin()->first << " " << m.rbegin()->second << endl;
cout << m.size() << endl; return 0; }
|
向量
vector是一个能够存放任意类型的动态数组,能增加和压缩数据
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 26 27 28 29 30
| vector<int>test; vector<int>test2(6); vector<int>test3(6, 3);
vector<vector<int>> res(n, vector<int>(n, 0));
test.push_back(5); test.push_back(8);
test.insert(test.begin()+i,value) ; test.erase(test.begin() + 5); test.clear(); test.size(); test.begin(); test.end();
for (vector<int>test::iterator it = m.begin(); it != m.end(); it++) { cout << *it << endl; }
reverse(test.begin(),test.end());
sort(test.begin(), test.end());
bool comp(const student &a, const student &b){ return a.score < b.score; } sort(test.begin(), test.end(), comp);
|