1、万能头文件

#include<bits/stdc++.h>

这个头文件包含了所有的头文件,以后只需要打这一个头文件就ok啦,注意hdu上只有选择G++才可以用

 

2、加速器

ios::sync_with_stdio(false);

加速cin和cout,这个可以加速到像scanf一样快,but省赛好像编译出错QAQ

 

3、stream流(巨尼玛好用)

(1)将字符串转换为数字

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s="123456";
	int t;
	stringstream flow(s);//放入流
	flow>>t;//将流输入到t
	//此时t就是整形的数字123456
	cout<<t;
        flow.clear();// stringstream清空,重复使用要记得清空 
}

  (3)将数字转换为字符串

    int number = 352412;
    stringstream flow;  
	string s;
    flow<<number;//放入流中 
    flow>>s;//放入字符串 
  
    cout << s << endl;
     flow.clear();// stringstream清空,重复使用要记得清空 

    //方法2:
        int a=132456;
	string s=to_string(a);//只支持c++11标准
	cout<<s;

至于DEV c++怎么调c++11:https://blog.csdn.net/qq_37597345/article/details/82741854?utm_source=blogxgwz2

(2)以空格分隔成字符串

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s="zbc vbn nmk",my;
	int t;
	stringstream flow(s);
	while(flow>>my)//分割 
	{
		cout<<my<<' ';
	}

	flow.clear();// stringstream清空,重复使用要记得清空 
}

 

4、sprintf(不支持string。。。)

#include<bits/stdc++.h>
using namespace std; 
int main()
{
    char str[256] = { 0 };
    int data = 1024; 
 	
    sprintf(str,"%d",data);  //将data转换为字符串
 	cout<<str<<endl;
 	
    sprintf(str,"0x%X",data);   //获取data的十六进制(X就会转为为大写十六进制) 
    cout<<str<<endl;
    
    sprintf(str,"0%o",data);   //获取data的八进制
    cout<<str<<endl;
    
    const char *s1 = "Hello"; 
    const char *s2 = "World";

    sprintf(str,"%s %s",s1,s2);   //连接字符串s1和s2
    cout<<str<<endl; 
    return 0;
}

 

5、string类型

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int i=0,n=2;
	string s="123456789";
	getline(cin,s);//输入带空格的字符串,以回车结束 
    
        s.length();//返回s的长度

	reverse(s.begin(),s.end()) ;//反转字符串
	
	s.substr(5,4);//返回第5个字符开始,长度为n的子串
	
	s.find(s,'2');//查找字符,没找到就返回-1 
	s.rfind(s,'3');//反向查找字符 
	s.find("123");//查找子串 
	
	/*
	说有三种用法:
	erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
	erase(position);删除position处的一个字符(position是个string类型的迭代器)
	erase(first,last);删除从first到last之间的字符(first和last都是迭代器) 
	*/
	string str ("This is an example phrase.");

  	// 第(1)种用法
  	str.erase (10,8);
  	cout << str << endl;        // "This is an phrase."

  	// 第(2)种用法
  	str.erase (9);    // "This is a phrase."
  	cout<<str; 

  	str.erase (str.begin()+5, str.end()-7);    // "This phrase."
  	cout << str << endl;
	return 0;
}

 

6、结构体初始化

#include<bits/stdc++.h>
using namespace std;
struct node
{
	int x;
	double y;
	string s;
 };
int main()
{
	node a=(node)
	{ .x=1, .y=2.0, .s="fuck you" };
	return 0;
}

 

7、排序

#include<bits/stdc++.h>
using namespace std;
struct node
{
	int x;
	double y;
	string s;
}a[100];

bool comp(node a,node b)//按照节点中某一个元素(x)对整个线性表进行排序 
{
	return a.x<b.x;
}

int main()
{
	int b[100] ;
	sort(b+i,b+n);//对[i,n-1]区间进行排序
	sort(a,a+n,comp) //按照自定义排序
	 
	random_shuffle(b,b+n);//随机打乱原来的序列
	 
	return 0;
}

 

8、bitset

#include<bits/stdc++.h>
#include<bitset>
using namespace std;
int main()
{
	int len=(int)log2(1315);
	bitset<len> bt(1315);//有len位, 将1315转换为二进制存入bt
	bitset<8> bs("00000111"); //直接存入 bs 
	cout<<bt; //可以直接输出 也可以输出bt[i](从高位到低位)
	
	bt.flip(0);   // bt[0]将按位取反 
	bt.flip();    // 全部按位取反
	
	 
	bt.count() ;//返回1的个数 
	return 0;
}

C++中那些开挂的东西

 

9、merge函数

这TM也是个神器啊,

它的用法是将两个有序数组合并到一个有序数组中,并使其也有序

但是使用merge函数必须要有一个条件,初始数组必须有序,并且要同为递增或递减

注意:默认只能合并递增序列,合并递减序列目前只能用结构体并重载小于操作符

合并递增序列:

#include<bits/stdc++.h>
using namespace std;
int a[5]={1,2,3,4,5};
int b[5]={2,4,6,8,10};
int c[10];
void print()
{
	cout<<"a : ";
	for(int i=0;i<5;i++) cout<<a[i]<<' ';
	
	cout<<endl<<"b : ";
	for(int i=0;i<5;i++) cout<<b[i]<<' ';
	
	cout<<endl<<"c : ";
	for(int i=0;i<10;i++) cout<<c[i]<<' ';
	
	cout<<endl<<endl;
}
int main()
{
	//sort(a,a+5); sort(b,b+5);
	
	merge(a,a+5,b,b+5,c);
	
	print();
	return 0;
}

C++中那些开挂的东西

合并递减序列:

#include<bits/stdc++.h>
using namespace std;
struct node
{
	int x;
	bool operator <(const node& t)//重写小于运算符
	{
		return x>t.x;
	}
}a[5],b[5],c[10];
void print()
{
	cout<<"a : ";
	for(int i=0;i<5;i++) cout<<a[i].x<<' ';
	
	cout<<endl<<"b : ";
	for(int i=0;i<5;i++) cout<<b[i].x<<' ';
	
	cout<<endl<<"c : ";
	for(int i=0;i<10;i++) cout<<c[i].x<<' ';
	
	cout<<endl<<endl;
}
int main()
{
	for(int i=0;i<5;i++)
	a[i].x=i,b[i].x=i+1;
	
	sort(a,a+5); sort(b,b+5);//排序,使其递减 
	
	merge(a,a+5,b,b+5,c);
	print();
	
	return 0;
}

C++中那些开挂的东西

这个也可以合并含有多个变量的结构体,一定要重写小于操作符,重载就相当于上面介绍的排序中的自定义排序函数。

10、STL不解释

STL:https://blog.csdn.net/qq_41431457/article/category/8758126

 

相关文章: