一些C++在算法的使用技巧

一些C++在算法的使用技巧

1. .#pragma GCC optimize(2)

#pragma GCC optimize(2)
//或者(O1、O3类似)
#pragma GCC optimize(“O2”)

C++手动开O2优化,O2优化能使程序的编译效率大大提升。
从而减少程序的运行时间,达到优化的效果。
PS:
O0:不做任何优化,这是默认的编译选项。
O1:提供基础级别的优化 。
O2:提供更加高级的代码优化,会占用更长的编译时间 。
O3:提供最高级的代码优化。
参考链接:传送门1

2. #include<bits/stdc++.h>

#include<bits/stdc++.h>

#include<bits/stdc++.h>包含了目前c++所包含的所有头文件, 会降低编译速度。偷懒写法(对初学者不推荐。)
参考链接:传送门2

3. #define INF 0x3f3f3f3f

#define INF 0x3f3f3f3f

0x3f3f3f3f的十进制是1061109567,是1e9级别的[和0x7fffffff(32-bit int的最大值)一个数量级],而一般场合下的数据都是小于1e9的,所以它可以作为无穷大使用而不致出现数据大于无穷大的情形。
用途:memset函数()把一段内存全部置为“无穷大”
参考链接:传送门3

4. ios::sync_with_stdio(false);

ios::sync_with_stdio(false);

cin,cout要先把要输出的东西存入缓冲区,再输出,导致效率降低,这段语句可以来打消iostream的输入 输出缓存,可以节省许多时间,使效率与scanf与printf相差无几。

5. cin.tie(0); 或者cin.tie(NULL);

 cin.tie(0); 
 //或者
 cin.tie(NULL);

tie是将两个stream绑定的函数,空参数的话返回当前的输出流指针。
在默认的情况下cin绑定的是cout,每次执行 << 操作符的时候都要调用flush,这样会增加IO负担。可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。
参考链接:传送门4、5

6. 简化的for循环

#define rep(i,a,b) for(register int i=(a);i<(b);++i)
#define ret(i,a,b) for(register int i=(a);i<=(b);++i)

简化的for循环,懒人写法,个人不推荐(不太好看、但是编写快)。
Ps: register关键字请求让编译器将变量a直接放入寄存器里面,以提高读取速度,在C语言中register关键字修饰的变量不可以被取地址,但是c++可以用 & 操作符取地址,一旦使用了取地址操作符,被定义的变量会强制存放在内存中。。c++中虽然支持register关键字,但是c++编译器也有自己的优化方式,即某些变量不用register关键字进行修饰,编译器也会将多次连续使用的变量优化放入寄存器中,如果自己指定了register变量,但是编译内器如果发现不合理,也会自动的取消程序员指定的register变量。

7. 整数/浮点快速读入

inline int read(void)
{
    int x=0,f=1;
	char c=getchar();
    while(c<'0'||c>'9')
	{
		if(c=='-')
			f=-1;
		c=getchar();
	}
    while(c>='0'&&c<='9')
		x=(x<<3)+(x<<1)+(c^48),c=getchar();
    return x*f;
}

将string和cstdio通过 手打函数 结合。
Ps:inline 可以把函数指定为内联函数。这样可以解决一些频繁调用的函数大量消耗栈空间(栈内存)的问题。
参考链接:传送门7

8. 快速打印

template <class T> inline void print(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar('0'+x%10);
}

参考链接:传送门8

-----------------------------------分割线-----------------------------------------
以上内容来自某大佬的一个代码片段,我只是将其查找,探寻一下作用,加深对其理解。
代码片段

#pragma GCC optimize(2)//开O2优化
#include<iostream>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<map>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<climits>
#include<list>
#include<stack>
#include<deque>
#include<sstream>
#include<unordered_map>
//#include<fstream>
//#include<windows.h>
//#include<bits/stdc++.h>
//priority_queue<int> A;
//priority_queue<int,vector<int>,greater<int> > B;
#define INF 0x3f3f3f3f//memset(num,0x3f,sizeof(num));
#define bug(x) printf("[%d]\n",(x));
//#define pause system("pause");
#define me(x,y) memset(x,y,sizeof(x));
#define EPS 0.0000000001
#define PI 3.1415926535897932385//cout<<PI=atan(1.0)*4;
#define EE 2.7182818284590452354//printf("%.20lf",exp(1));
#define RR 0.57721566490153286060651209//欧拉常数 
#define it iterator
#define rint register int
#define rep(i,a,b) for(register int i=(a);i<(b);++i)
#define ret(i,a,b) for(register int i=(a);i<=(b);++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()
typedef long long ll;
using namespace std;
//inline int min(int a,int b){return a<b?a:b;}
//
//inline int max(int a,int b){return a>b?a:b;}
inline int read(void)
{
    int x=0,f=1;
	char c=getchar();
    while(c<'0'||c>'9')
	{
		if(c=='-')
			f=-1;
		c=getchar();
	}
    while(c>='0'&&c<='9')
		x=(x<<3)+(x<<1)+(c^48),c=getchar();
    return x*f;
}
template <class T> inline void print(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar('0'+x%10);
}
//int dx[]={0,0,1,-1,1,-1,1,-1};
//int dy[]={1,-1,0,0,1,-1,-1,1};
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	return 0;
} 
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值