1.常用最大数值
0x3f:代表的数值63
0x3f3f3f3f:代表的数值1061109567 10的九次方多
2.用sort对结构体排序
定义头文件
#include<bits/stdc++.h>
using namespace std;
定义结构体
struct rt {
int x,y,z;
} sb[100];
使用sort函数需定义的自建函数(这里的a,b可以换成其他的数,别忘了加地址符,通过排序sb[i].z来排序结构体sb[i])
注意:如果是'<'那就是把从小到大来排序,如果是'>'那就是从大到小来排序
bool cmp(struct rt &a,struct rt &b)
{
return a.z<b.z;
}
正式使用(cmp目的旨在告诉按什么方式排序)
sort(sb+1,sb+1+m,cmp);
3.结构体的方便使用
struct node { //结构体的使用
int x;
int y;
double z;
} q[N];
快速填入对应的数字。
q[++k]=(node) {x,y,z};
4.pair的常用用法
还有其他的pair用法,我现在没用上,用上的时候在加。
队列
queue<pair<int,int> >q; //queue指队列,pair的使用是为了让队列里有两个数据,正常情况下队列里只能有一个,现在也可以是 <string,int>q <char,double>q
入栈
q.push(make_pair(x,y));
字符标记
int xx=q.front().first,yy=q.front().second; //英语,first指第一个,second指第二个,
5.string求长度的用法。
sting nums。那么其长度为nums.size(),其数组也一样适用。
6.函数的特殊返回值
1.返回两个数
则用return {i,j};
2.返回string的部分字符串。
return nums.substr(0,i+1);字符串名为nums,substr为string头文件里面的函数,返回范围是0到 i 。
7.vector
vector题目:洛谷P5318【深基18.例3】查找文献
8.map映射
输入:
5
abc
aaaa
abc
abcc
12345
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<string,int>a; //map定义,相当于数组的升级版,表示string到int的映射,举个简单例子,可以用a["OK"]=1;进行赋值(说白了就是括号里的数据类型是map<>前面的,=的后面就是map<>后面的数据类型)
string s;//用于读入的字符串
int n,m;
int main(){
cin>>n;
m=n;
while(n--){//因为是n+m就可以了,所以直接n--,--QWQ
cin>>s;
if(a[s]==1)m--;
else
a[s]=1;
}
printf("%d",m);
return 0;//完美的过程,不是吗?
}
9.accumulate函数
使用前需要引入相应的头文件。
#include <numeric>
-
函数共有四个参数,其中前三个为必须,第四个为非必需。
-
若不指定第四个参数,则默认对范围内的元素进行累加操作。
accumulate(起始迭代器, 结束迭代器, 初始值, 自定义操作函数)
应用场景
1. 计算数组中所有元素的和
#include <iostream>
#include <numeric>
using namespace std;
int main() {
vector<int> arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = accumulate(arr.begin(), arr.end(), 0); // 初值0 + (1 + 2 + 3 + 4 +... + 10)
cout << sum << endl; // 输出55
return 0;
}
2. 计算数组中所有元素的乘积
需要指定第四个参数,这里使用的是乘法函数 multiplies<type>()
, type根据元素的类型选择。
#include <iostream>
#include <numeric>
using namespace std;
int main() {
vector<int> arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = accumulate(arr.begin(), arr.end(), 1, multiplies<int>()); // 初值1 * (1 * 2 * 3 * 4 *... * 10)
cout << sum << endl; // 输出3628800
return 0;
}
3. 计算数组中每个元素乘以3之后的和
#include <iostream>
#include <numeric>
using namespace std;
int fun(int acc, int num) {
return acc + num * 3; // 计算数组中每个元素乘以3
}
int main() {
vector<int> arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = accumulate(arr.begin(), arr.end(), 0, fun);
cout << sum << endl; // 输出 165
return 0;
}
4.计算数组中每个元素减去3之后的和
#include <iostream>
#include <numeric>
using namespace std;
int fun(int acc, int num) {
return acc + (num - 3) ; // 计算数组中每个元素减去3之后的和
}
int main() {
vector<int> arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = accumulate(arr.begin(), arr.end(), 0, fun);
cout << sum << endl; // 输出25
return 0;
}
5.计算班级内学生的平均分
#include <iostream>
#include <numeric>
using namespace std;
struct Student {
string name;
int score;
Student() {}; // 无参构造函数
Student(string name, int score) : name(name), score(score) {}; // 有参构造函数
};
int fun(int acc, Student b) {
return acc + b.score;
}
int main() {
vector<Student> arr;
arr.emplace_back("Alice", 82);
arr.emplace_back("Bob", 91);
arr.emplace_back("Lucy", 85);
arr.emplace_back("Anna", 60);
arr.emplace_back("June", 73);
int avg_score = accumulate(arr.begin(), arr.end(), 0, fun) / arr.size(); // 总分/学生数
cout << avg_score << endl;
return 0;
}