C++ 基本数据结构
C++ 基本数据结构
动态数组 vector
初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <vector>
int n = 7, m = 8;
vector<int> nums;
vector<int> nums(n);
vector<int> nums{1, 3, 5};
vector<int> nums(n, 2);
vector<vector<int>> dp;
vector<vector<bool>> dp(m, vector<bool>(n, true));
|
常用操作
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 39 40 41 42 43 44 45 46 47 48
| #include <iostream> #include <vector> using namespace std;
int main() { int n = 10; vector<int> nums(n); cout << nums.empty() << endl; cout << nums.size() << endl;
nums.push_back(20); cout << nums.size() << endl;
cout << nums.back() << endl;
nums.pop_back(); cout << nums.size() << endl;
nums[0] = 11; cout << nums[0] << endl;
nums.insert(nums.begin() + 3, 99);
nums.erase(nums.begin() + 2);
swap(nums[0], nums[1]);
for (int i = 0; i < nums.size(); i++) { cout << nums[i] << " "; } cout << endl; }
|
双链表 list
初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <list>
int n = 7;
std::list<int> lst;
std::list<int> lst(n);
std::list<int> lst{1, 3, 5};
std::list<int> lst(n, 2);
|
常用操作
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 39 40 41 42 43 44 45 46 47 48 49 50
| #include <iostream> #include <list> using namespace std;
int main() { list<int> lst{1, 2, 3, 4, 5};
cout << lst.empty() << endl;
cout << lst.size() << endl;
lst.push_front(0); lst.push_back(6);
cout << lst.front() << " " << lst.back() << endl;
lst.pop_front(); lst.pop_back();
auto it = lst.begin(); advance(it, 2); lst.insert(it, 99);
it = lst.begin(); advance(it, 1); lst.erase(it);
for (int val : lst) { cout << val << " "; } cout << endl;
return 0; }
|
队列 queue
初始化
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
| #include <iostream> #include <queue> using namespace std;
int main() { queue<int> q;
q.push(10); q.push(20); q.push(30);
cout << q.empty() << endl;
cout << q.size() << endl;
cout << q.front() << " " << q.back() << endl;
q.pop();
cout << q.front() << endl;
return 0; }
|
栈 stack
初始化
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 <stack> using namespace std;
int main() {
stack<int> s;
s.push(10); s.push(20); s.push(30);
cout << s.empty() << endl;
cout << s.size() << endl;
cout << s.top() << endl;
s.pop();
cout << s.top() << endl;
return 0; }
|
哈希表 unordered_map
初始化
1 2 3 4 5 6 7 8
| #include <unordered_map> using namespace std;
unordered_map<int, string> hashmap;
unordered_map<int, string> hashmap{{1, "one"}, {2, "two"}, {3, "three"}};
|
常用操作
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include <iostream> #include <unordered_map> using namespace std;
int main() { unordered_map<int, string> hashmap{{1, "one"}, {2, "two"}, {3, "three"}};
cout << hashmap.empty() << endl;
cout << hashmap.size() << endl;
if (hashmap.contains(2)) { cout << "Key 2 -> " << hashmap[2] << endl; } else { cout << "Key 2 not found." << endl; }
cout << hashmap[4] << endl;
hashmap[4] = "four";
cout << hashmap[4] << endl;
hashmap.erase(3);
if (hashmap.contains(3)) { cout << "Key 3 -> " << hashmap[3] << endl; } else { cout << "Key 3 not found." << endl; }
for (const auto &pair: hashmap) { cout << pair.first << " -> " << pair.second << endl; }
unordered_map<int, string> hashmap2;
cout << hashmap2.size() << endl;
cout << hashmap2[1] << endl; cout << hashmap2[2] << endl;
cout << hashmap2.size() << endl;
return 0; }
|
哈希集合 unordered_set
初始化
1 2 3 4 5 6 7 8
| #include <unordered_set> using namespace std;
unordered_set<int> uset;
unordered_set<int> uset{1, 2, 3, 4};
|
常用操作
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 39 40 41 42 43 44 45 46
| #include <iostream> #include <unordered_set> using namespace std;
int main() { unordered_set<int> hashset{1, 2, 3, 4};
cout << hashset.empty() << endl;
cout << hashset.size() << endl;
if (hashset.contains(3)) { cout << "Element 3 found." << endl; } else { cout << "Element 3 not found." << endl; }
hashset.insert(5);
hashset.erase(2); if (hashset.contains(2)) { cout << "Element 2 found." << endl; } else { cout << "Element 2 not found." << endl; }
for (const auto &element : hashset) { cout << element << endl; }
return 0; }
|
树
二叉树结构
1 2 3 4 5 6 7 8 9
| struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right(nullptr) {} TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} };
|
二叉树 DFS 深度优先搜索
前序遍历(根->左->右)
1 2 3 4 5 6 7
| void DFS_Preorder(TreeNode* root) { if (!root) return; cout << root->val << " "; DFS_Preorder(root->left); DFS_Preorder(root->right); }
|
中序遍历(左->根->右)
1 2 3 4 5 6 7
| void DFS_Inorder(TreeNode* root) { if (!root) return; DFS_Inorder(root->left); cout << root->val << " "; DFS_Inorder(root->right); }
|
后序遍历(左->右->根)
1 2 3 4 5 6 7
| void DFS_Postorder(TreeNode* root) { if (!root) return; DFS_Postorder(root->left); DFS_Postorder(root->right); cout << root->val << " "; }
|
二叉树 BFS 广度优先搜索(层序遍历)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| void levelOrder(TreeNode* root) { if (!root) return; queue<TreeNode*> q; q.push(root); while (!q.empty()) { TreeNode* node = q.front(); q.pop(); cout << node->val << " "; if (node->left) { q.push(node->left); } if (node->right) { q.push(node->right); } } cout << endl; }
|
版权声明: 此文章版权归 AngFff 所有,如有转载,请注明来自原作者
赞助

微信

支付宝