#include <algorithm>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
//刷题中的一些语言基础
int main() {
// 1.数组
vector<int> nums; //初始化一个int的空数组
vector<int> nums1(5); //初始化大小是5的数组,默认值为0
vector<int> nums2{1, 3, 4}; //初始化元素是1,3,4的数组
vector<int> nums3(5, 2); //初始化一个大小是5,默认值是2的数组
vector<vector<int>> dp; //二维int数组dp
vector<vector<bool>> dp2(
3, vector<bool>(2, true)); //初始化一个3*2数组且默认值true
cout << nums2.empty() << endl; // false;
cout << nums2.size() << endl;
nums.push_back(20);
int b = nums.back(); // b=20 获取到数组最后一个元素的引用
nums.pop_back(); //删除最后一个元素,无返回值
swap(nums2[0], nums2[1]); //交换这两个元素的值
// 2.字符串
string s;
s = "abcd";
s.push_back('e');
cout << s.size(); // 5
cout << s.substr(2, 3); //返回从索引2开始,长度为3的子字符串
s += "xyz";
// 3.哈希表 unordered_map
vector<int> num{1, 2, 3, 4, 4};
unordered_map<int, int> counter;
for (int n : nums3) {
counter[n]++;
}
for (auto &it : counter) {
int key = it.first;
int value = it.second;
}
// empty()返回是否为空
// count(const &key)函数,返回是否存在,存在1,不存在0
// erase(key &)删除元素
// 4.哈希集合 unordered_set
/*
empty()返回是否为空
count(const &key)函数,返回是否存在,存在1,不存在0
插入元素insert(const &key)插入元素
erase(key &)删除元素*/
//5.队列queue
queue<int> q;
q.empty();
q.push(2);
q.pop();
q.front(); //会返回引用
//C++中的pop方法都是void类型,所以一把都是以下做法:
int e = q.front();
q.pop();
//6.堆栈stack
stack<int> stk;
stack<string> stk2;
stk.empty();
stk.push(2);
stk.pop();
stk.top(); //返回栈顶元素的引用