加载中...

竞赛常用基础

语法基础

万能头文件

#include<bits/stdc++.h>
//这行代码包含整个标准库 (bits/stdc++.h)。它是一个方便的头文件,在竞技编程环境中经常使用,它包含了大多数标准头文件。

取消同步流

#include<bits/stdc++.h>
using namespace std;
int main(){
	ios::sync_with_stdio(0);//关闭c++和c的输入输出流同步 
	//解除cin 和 cout的绑定 
	cin.tie(0);
	cout.tie(0);
} 

替换ENDL

	cout << a << '\n'; //比endl 处理快

控制格式化

#include<bits/stdc++.h>
using namespace std; 
int main(){
	double a, b;
	a = 5;
	b = 6;
	cout << fixed << setprecision(3) << a << b << '\n'; //控制三位小数点 若需要填充需配合fixed
}

全局数组的使用

#include<bits/stdc++.h>
using namespace std; 
const int N = 1000; //编译器常量 
int a[N]; //全局数据默认为0 不必初始化 存储在全局静态存储区 

int main(){
	for(int i=0;i<N;i++){
		cout << a[i] << endl;
	}
}

常用库函数

排序

sort(a,a+n+1); // 数组名
sort(v.begin(),v.end()) //vector数组
    
//自定义比较函数形式
 #include<bits/stdc++.h>
using namespace std; 

//二元谓词
bool cmp(const int& i,const int& j){
	return i > j; //排序方式 默认 i < j 
}
int main(){
	vector<int> v = {1,3,7,11};
	sort(v.begin(),v.end(),cmp);
	for(auto c : v)
		cout << c << " "; 
 /*11 7 3 1
--------------------------------
Process exited after 0.01275 seconds with return value 0
请按任意键继续. . .*/

    
//自定义比较lambda
#include<bits/stdc++.h>
using namespace std; 

int main(){
	vector<int> v = {1,3,7,11};
	sort(v.begin(),v.end(),[](int i,int j){return i > j;});
	for(auto c : v) //c++ 11
		cout << c << " "; 
}
}

最值查找

#include <bits/stdc++.h>
using namespace std;
int main()
{
  	int a = 5, b = 6;
  	//要么参数 要么列表 
  	int Maxelem = max(a,b);
  	int Minelem = min(a,b);
  	int Big = max({1,3,4,6});
  	
  	cout << Maxelem << endl;
  	cout << Minelem << endl;
  	cout << Big << endl;
  	
  	vector<int> v = {1,8,22,3,9,11};
  	//返回地址 
  	Maxelem = *max_element(v.begin(),v.end());
  	cout << Maxelem << endl;
  	Minelem = *min_element(v.begin(),v.end());
	cout << Minelem << endl;
	// 对指定位置进行排序好 左右只是比其小 
	nth_element(v.begin(),v.begin()+2,v.end());
	for (auto c : v ){
		cout << c << " ";
	} 	
}

二分查找

#include <bits/stdc++.h>
using namespace std;
int main()
{
  int n;
  vector<int> v(n);
  for (int i = 0; i < n ; ++i)   cin >> v[i];
  int target;
  cin >> target;
  bool found = binary_search(v.begin(),v.end(),target);
  
  cout << found << endl;
  lower_bound(v.begin(),v.end(),target);//找到第一个大于或者等于 地址 -begin 
  upper_bound(v.begin(),v.end(),target); //找到第一个大于	地址 -begin 
}

大小写转换

#include <bits/stdc++.h>
using namespace std;
int main()
{
	char c1 = 'A';
	char c2 = 'a';
	
	bool a = islower(c1);
	bool b = isupper(c2);
    char c = tolower(c1) ; //需要定义 不然返回的ASCII
	cout << c;
}

全排列

#include <bits/stdc++.h>
using namespace std;
int main()
 //会变成原样
{
    //返回bool
	vector<int> v = {1,2,3};
	while(next_permutation(v.begin(),v.end())){
		for (auto c : v) cout << c << " ";
		cout << endl;
	}
	
}

#include <bits/stdc++.h>
using namespace std;
int main()
{
	//上一个全排列 
	vector<int> v = {3,2,1};
	while(prev_permutation(v.begin(),v.end())){
			for (auto c : v) cout << c << " ";
			cout << endl;
		}
	
}

mernset

//void *memset(void *ptr, int value, size_t num);
#include <bits/stdc++.h>
using namespace std;
int main()
{
	//按1B设置第二个参数
	//常用两种 
	int a[6];
	memset(a,0,sizeof(a));// point 设置的num 对多大的大小 
	for( auto c:a) cout << c << " ";
	int b[4];
	memset(b,-1,sizeof(b));
	for( auto c:b) cout << c << " "; 
}

SWAP

//对象交换this指针
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int a = 5, b = 6;
	swap(a,b);
	cout << a << " " << b; 
}

reverse

#include <bits/stdc++.h>
using namespace std;
int main()
{
	vector<int> v = {1,3,2,2346};
	reverse(v.begin(),v.end());
	for (auto c:v) cout << c << " ";
}

unique

#include <bits/stdc++.h>
using namespace std;
int main()
{
	vector<int> v = {1,2,2,2,3,46};
	auto it = unique(v.begin(),v.end());//返回处理后末尾元素的地址 
	
//	for (auto c : v) cout << c << " ";
	// 1 2 3 46 3 46 覆盖去重
	v.erase(it,v.end());
	for (auto c : v) cout << c << " ";
}
posted @ 2024-02-24 13:54  江寒雨  阅读(70)  评论(0)    收藏  举报