刷leetcode常用的c++知识(留着自己用)
参考
https://www.bilibili.com/video/av42138586?from=search&seid=10392605227757769251
https://blog.csdn.net/Echo_dia/article/details/100049663
https://blog.csdn.net/qq_42659468/article/details/90381985
algorithm
常用成员函数:
swap
int main(){
string s1 = "aaa", s2 = "bbb", s3 = "ccc";
swap(s1,s2);
cout << s1 << endl << s2; // s1 = "bbb", s2 = "aaa"
return 0;
}
min,max同前
int main(){
string s1 = "aaa", s2 = "bbb", s3 = "ccc";
string s4 = min(s1,s2);
cout << s4; // aaa
return 0;
}
sort
这里sort(a,a+9)是左闭右开区间,即不包括第9个,下标从0开始
若从1开始sort(a+1,a+1+9)
int main(){
int a[] = {5,1,2,6,9,7,6,8,4,3};
sort(a,a+9);
for(int i = 0; i < 10; i++){
cout << a[i] << " "; // 1 2 4 5 6 6 7 8 9 3
}
return 0;
}
从大到小排序
bool cmp(int a, int b)
{
return a > b;
}
int main(){
int a[] = {5,1,2,6,9,7,6,8,4,3};
sort(a,a+10,cmp);
for(int i = 0; i < 10; i++){
cout << a[i] << " "; // 9 8 7 6 6 5 4 3 2 1
}
return 0;
}
next_permutation
int main(){
int b[] = {1,2,3};
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 3; j++)
{
cout << b[j];
}
cout << endl;
next_permutation(b,b+3);
}
return 0;
}
/*
123
132
213
231
312
321
123
132
*/
unique
作用是去除重复元素,需要先排序,左闭右开,返回去重后的最右端
int main(){
int a[] = {5,1,2,6,9,7,6,8,4,3};
sort(a,a+10);
int c = unique(a,a+10)-a;
cout << c << endl; // 9
for(int i = 0; i < 10; i++){
cout << a[i] << " "; // 1 2 3 4 5 6 7 8 9 9
}
return 0;
}
binary_search
需要先排序
int main(){
int a[] = {5,1,2,6,9,7,6,8,4,3};
sort(a,a+10); // 排序很重要
int c = binary_search(a,a+10,2);
cout << c << endl; //成功返回1,失败则返回0
return 0;
}
lower_bound
返回第一个大于等于某个元素的位置
int main(){
int a[] = {5,1,2,6,9,7,6,8,4,3};
sort(a,a+10);
int c = lower_bound(a,a+10,3) - a;
cout << c << endl; // 2
return 0;
}
upper_bound
查找第一个大于某个元素的位置
int main(){
int a[] = {5,1,2,6,9,7,6,8,4,3};
sort(a,a+10);
int c = upper_bound(a,a+10,3) - a;
cout << c << endl; // 3
return 0;
}
容器类(链表,队列。。。)
通用操作
- size() 大小
- empty() 是否空
- begin() 指向第一个的迭代器
- end() 指向末尾迭代器,内容的最后一个的再往后一个,是非法内容
- erase() 删除
- insert() 插入
- clear() 清空,栈和队列没有
迭代器
string,vector, deque提供随机访问迭代器(可以直接+m找到某个,而不需逐步+1直至到某个元素),list, set, multiset, map, multimap提供双向迭代器
// 假设有某个vector v1
vector<int>::iterator it; // 声明一个迭代器
for(it = v1.begin(); it != v1.end(); it++)
cout << *it;
vector
插入删除平时不用,一般push_back(),pop_back()
//vector初始化可以 vector<int> a = {1,2,3};
int main(){
vector<int> v;
cout << v.empty() << endl; // 1
vector<char> v2(3,'a'); // 三个 a
cout << v2.size() << endl; // 3
for(int i = 0; i < v2.size(); i++){
cout << v2[i];
}
cout << endl; // aaa
v2.clear();
cout << v2.empty() << endl; // 1
v2.push_back('A');
v2.push_back('B');
for(int i = 0; i < v2.size(); i++){
cout << v2[i];
}
cout << endl; // AB
cout << v2.front() << v2.back() << endl; // AB
v2.resize(1); // 重置大小
for(int i = 0; i < v2.size(); i++){
cout << v2[i];
}
cout << endl; // A
return 0;
}
queue
常用 front() 返回第一个,back() 返回最后一个,push() 加入一个, pop() 弹出一个
priority_queue:优先队列(默认大顶堆),top() 返回堆顶元素,push() 加入元素, pop() 弹出元素
// 三个参数:存放元素类型,用什么存(默认vector),按什么操作排序(默认less),less和greater都在<functional> 中
priority_queue<int, vector<int>, greater<int> >pq; // 小顶堆
int main(){
int a[] = {2,5,1,3,6,4};
vector<int> v(a,a+6);
queue<int> q;
priority_queue<int> pq;
for(int i = 0; i < v.size(); i++)
{
q.push(v[i]);
pq.push(v[i]);
}
while(!q.empty())
{
cout << q.front();
q.pop();
}
cout << endl;
//251364
while(!pq.empty())
{
cout << pq.top();
pq.pop();
}
// 654321
return 0;
}
stack
操作 push(), pop(), top(), 这里操作和queue类似,不写了
set
这里比较懒,直接参考了一位博主,地址贴在最顶上了
set中的元素是排好序的,元素不重复
用法
set<int> my_set;
my_set.insert(a);
my_set.erase(iter); //输入迭代器
my_set.clear();
my_set.empty();
my_set.begin();
my_set.end();
my_set.size();
my_set.find(a); //返回迭代器
my_set.rbegin(); // my_set.rbegin() = my_set.end()-1;
my_set.rend(); // my_set.rend() = my_set.begin()+1;
my_set.lower_bound(key_value) //返回第一个大于等于key_value的定位器
my_set.upper_bound(key_value) //返回最后一个大于key_value的定位器
例子
#include<iostream>
#include<set>
using namespace std;
int main(){
set<int> my_set;
int num[5] = {1,5,9,8,1};
for(int i=0;i<5;++i)
my_set.insert(num[i]);
auto iter = my_set.begin();
while(iter != my_set.end())
{
cout << *iter << ' '; // 1 5 8 9
iter++;
}
cout << endl;
// cout << my_set[0] << endl;
auto iterf = my_set.find(9);
cout << *iterf << endl; // cout: 9
my_set.erase(8); // 1 5 9
my_set.erase(iterf); // 1 5
my_set.clear();
cout << my_set.empty() << endl; // cout: 1
set<int> my_set1(num,num+5); // 1 5 8 9 另一种初始化方式
auto it1 = my_set1.lower_bound(1);
auto it2 = my_set1.upper_bound(1);
cout << *it1 << endl; // 1
cout << *it2 << endl; // 5
return 0;
}
map
每个节点都是一个pair,成员变量first,second对应key,value
int main(){
map<string, int>mp;
mp["Sunday"] = 1;
mp["Monday"] = 2;
cout << mp["Monday"] << endl; // 2
map<string, int>::iterator it = mp.find("Sunday");
cout << it->first << endl; // Sunday
return 0;
}
string
- string str——构造空的string类对象,即空字符串
- string str(str1)——str1 和 str 一样
- string str(“ABC”)——等价于 str=“ABC”
- string str(srelen,‘A’)——存储 strlen 个 ‘A’ 到 str 中
- str.length()——求字符串长度
- str.size()——和 length() 一样
- str.find(‘A’)——查找 ‘A’
- str.find(“ABC”)——查找 “ABC”
- str.find(‘B’,1)——从 位置1 处,查找’B’
- str1=str.substr(2)——提取子串,提取出 str 的 下标为2 到末尾,给 str1
- str1=str.substr(2,3)——提取子串,提取出 str 的 下标为2 开始,提取三个字节,给 str1
- push_back和popback,与vector类似