关于auto关键字:
auto i= a;(a是啥i就是啥类型)
int b[10] = {1,2,3,4,5,6};
for(auto &x:b) cout << x<< endl;
兼容stl 要注意带& 这样才是直接访问数组元素
关于结构体的初始化和赋值
#include <bits/stdc++.h>
using namespace std;
struct Node{
int a,b,c;
Node(){a=1,b=2,c=3;};
Node(int A,int B,int C) {
a=A,b=B,c=C;
}
};
int main() {
Node t;
printf("%d %d %d\n", t.a, t.b, t.c);
t = {4,5,6};
printf("%d %d %d\n", t.a, t.b, t.c);
t = Node(7,8,9);
printf("%d %d %d\n", t.a, t.b, t.c);
return 0;
}
输出结果:
1 2 3
4 5 6
7 8 9
关于队列的清空:
queue<int> q;
q = queue<int>(); //此法很禽兽 会让用while()清空的同学很难受
关于初始化的一些问题:
类不能用memset memcpy等函数初始化 用clear()
读入整行
char s[1001];
cin.getline(s,1000);
string s;
getline(cin,s);
冷门小知识:键盘上pause/break 的作用:
这个按键有点难找 在page up 的上面 (有的键盘可能没有)它的作用是在你跑程序的时候 按一下可以暂停 然后按任意键继续运行
不管是c中的strlen 还是 c++中的size,返回值都是无符号整数
关于优先队列的使用:
#include <bits/stdc++.h>
using namespace std;
struct cmp {
bool operator ()(int x, int y) {
return x > y;//小的优先级高
}
};
int main() {
priority_queue<int> q1; // 默认大的优先级高
priority_queue <int,vector<int>, cmp> q2;
/*
或者直接定义成
priority_queue <int,vector<int>, less<int> > q1;大的优先级高
priority_queue <int,vector<int>, greater<int> > q2;小的优先级高
*/
q1.push(1);q1.push(2);
cout << q1.top() <<endl;
q2.push(1);q2.push(2);
cout << q2.top() <<endl;
return 0;
}
另外 优先队列访问队列头要用top 不能用front
精度问题要考虑用eps -0.00是个坑