1、返回数组排序后的元素对应的下标
https://blog.csdn.net/coolsunxu/article/details/109999132
2、初始化string为n个相同的字符 = string s(5,’a’);
3、 Char -> string = string.push_back(character)
4、⚠️ 一行未知个数的输入:当做字符串处理
(1)getline,以\n结尾输入一行⚠️ string s;getline(cin,s);✅ 前面数字输入之后,需要getchar()吃掉末尾的空格,然后才是geline(cin,s),这样是正常输入s
(2)然后对字符串进行处理
5、排序cmp函数的实现,对于PSI这种自定义数据类型
(1) lc 版本1
[&]是lamber式子的符 = static bool cmp
sort(strs.begin(),strs.end(),[&](pair<string,int>& a,pair<string,int>& b){
for(int i = a.first.size() - trim;i < a.first.size();i++){
if(a.first[i] < b.first[i]){//小的排在前面,true
return true;
}else if(a.first[i] > b.first[i]){
return false;
}
}
return a.second < b.second;
});//相当于把cmp写在了sort里面
(2)lc版本2
static bool cmp(pair<string,int>& a,pair<string,int>& b){
for(int i = a.first.size() - trim;i < a.first.size();i++){
if(a.first[i] < b.first[i]){//小的排在前面,true
return true;
}else if(a.first[i] > b.first[i]){
return false;
}
}
return a.second < b.second;
}
(3)acm版本
少了一个static
bool cmp(pair)
(4)acm自定义类型的重载小于号
bool operator < (Edge const& t) const{
return w < t.w;
}
6、⚠️ s.substr(I,len)复杂度多少?✅ Its time complexity is O(N) where N is the size of the substring.
7、遍历PII,
for(auto [c,k] : hash){
res += (k / 2 * 2);
}
8、char转换成string
string res = “”;
res.push_back(s[0]);
9、初始化string固定长度,并且设置成指定的字符
string s5(10, ‘c’);
10、lc的struct的定义,在public下面
public:
struct Node{
string food_name;
string cuisine;
int rate;
}a[N];
11、next_permutation的使用
(1)要先对容器进行升序排列
(2)用do{}while(next_permutation),因为就是第一个排序的vector是需要先在do里面进行处理的
12、nth_elements(a,begin(),midptr,a.end())
(1)x = *midptr
(2)左边是<=x的数,右边是>x的数字
13、对于NxN的格子,按照从左上到右下的方式斜线填写的代码的逻辑
感性理解:
每次斜线填写行都是从0~n - 1填写,所以是正常i++
每次斜线填写都是j = (j + 1) % n
然后i == n表示填完了当前的这个斜线的0~n - 1的位置,所以就是可以i = 0,然后j = (j + 1) % n就是移动到下一个斜线的开始的位置
let p=0 , q=0
for i = 1..k
Change A[p+1][q+1] into 1
let p=(p+1) , q=(q+1)%n
if(p==n)
let p=0 , q=(q+1)%n
14、判断奇数偶数
if(x & 1)
if(!(x & 1)) 偶数
❎ if()x & 1 == 0偶数,这是错误的
15、c的数组
(1)定义二维数组:int a[8][3] = {{},{},{}}
(3)数组作为参数
① 一维数组:不用设置数字
int compute(int array[4]);
int compute(int array[]);
int compute(int *array); // 编译器会把上面两个解释成这个
以上三种原型都会被编译器解释为指针,因此即使在数组定义里写了长度为4,依然可以传任意长度的数组进来。
② 二维数组:除了第一维不需要设置数字,其他维度都需要设置数字✅ 这是二维数组作为参数的特别重要的地方
void f(int a[][3]){
printf(“test”);
}
16、set的使用
set中的元素只能通过迭代器访问,只能顺序访问,就是访问第i个元素,只能for循环来s.begin()次数i次之后,才能访问到,没有随机访问❎ s.begin() + i的操作是不允许的