刷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類似