C/C++零碎知识点整理(不断更新)

关于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是个坑

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值