c++入门:分享一些实用的函数

分享一些实用的函数

我们的高级语言期末考试很快就要到了,在这里给大家分享一些我所知道的好用的函数。

1. Sort函数

void sort (first, last, cmp),包含在头文件algorithm内;
第三个参数cmp指排序方法。如果第三个参数不写,则默认为从小到大排序

用法1:数组排序

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
   
   
    int a[6]={
   
   5,2,7,4,1};
    sort(a,a+5);//a[0]--a[4]排序,默认从小到大排序;a[1]--a[5]排序则写成 :sort(a+1,a+6);
    for(int i=0;i<5;++i)printf("%d ",a[i]); //运行结果为1 2 4 5 7 
    return 0;
}

自定义比较函数:
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a,int b){
   
   return a>b;} //bool型函数的返回结果为 :若a>b,True; 否则,False; 降序排序; 
int main(){
   
   
   int a[6]={
   
   5,2,7,4,1};
   sort(a,a+5,cmp);
   for(int i=0;i<5;++i)printf("%d ",a[i]); //此时运行结果为7 5 4 2 1	 
   return 0;
}

用法2:字符串排序

#include<iostream>
#include<algorithm>
using namespace std;
string s1,s2; 
int main(){
   
   
	s1="52741";
	sort(s1.begin(),s1.end());
	cout<<s1<<endl; //输出结果12457
//大小字母也可以sort,以ascii码大小排序
	s2="fctaAPB";
	sort(s2.begin(),s2.end());
	cout<<s2<<endl; //输出结果ABPacft
    return 0;
}

用法3:结构体排序

#include<iostream>
#include<algorithm>
using namespace std;
struct Student{
   
   
	char name[20];
	int index,val;
}stu[100];
bool cmp(Student a,Student b){
   
   
	if(a.val!=b.val)return a.val>b.val;//分数大的排前; 
	if(a.name!=b.name)return a.name<b.name;//分数相同,以字典序排序; 
	return a.index<b.index;//名字相同,以输入先后顺序排序; 
}
int main(){
   
   
	//输入略; 
	sort(stu,stu+n,cmp);
	//输出略; 
    return 0;
}

2. Find函数

这里我们所说的find函数指string中的find函数,头文件为:string;
基本函数有
find()
find_first_of()
find_last_of()


find函数在字符串操作题中很常用,可与strsub等函数结合使用。


#include<iostream>
#include<cstring>
using namespace std;
int main(){
   
   
	string s="012a45a7a9";
	int  pos_start = s.find_first_of("a");//find_first_of即为从头至尾找第一个符合元素 
	int  pos_last= s.find_last_of("a") ;// find_last_of即找最后一个符合元素 
	cout<<pos_start<<"  "<<pos_last<<endl;//输出结果:3  8; 
	cout<<s[pos_start]<<"  "<<s[pos_last]<<endl; //输出结果:a  a
	//综上,find函数返回值是下标(从0开始); 
	int  xpos=s.find_first_of("a",5) ;//从第五个字符开始找第一个符合元素 
	cout<< xpos <<endl;//输出结果:6 
		
	//当find函数无法找到时,其返回string::npos ,一般而言,即为-1; 
	bool f1=s.find("x")==s.npos;
	bool f2=s.find("b")!=s.npos;//可以将s.npos简单理解成-1,即如果该字符串无法找到,返回-1; 
	cout<< f1 <<"  "<< f2 <<endl;//输出结果为:1  0;(即f1中等式为false,f2中为true) 用-1替代s.npos结果相同;
    return 0;
}

3. Reverse函数

void reverse (BidirectionalIterator first,BidirectionalIterator last);头文件:algorithm
reverse函数可以将数组字符串,vector等直接逆序,在一些解题中也十分方便。

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
int main(){
   
   
	int a[5];
	for(int i=0;i<5;++i)a[i]=i; //赋值为0 1 2 3 4 
	reverse(a,a+5);  //reverse(a+x,a+y+1) 从a[x]到a[y]逆序; 
	for(int i=0;i<5;++i)printf("%d ",a[i]);  
	puts("");//换行
	//输出结果为4 3 2 1 0 
	
	string s="01234";
	reverse(s.begin(),s.end());
	cout<<s<<endl;
	//输出结果:43210
	
	vector<int> v={
   
   0,1,2,3,4};
	reverse(v.begin(),v.end());
	for(auto x:v)cout<<x<<" ";
	//输出结果:4 3 2 1 0 
	
    return 0;
}

4.Stringstream

stringstream是c++提供的串流(stream)物件,头文件:sstream;我们可以简单地使用它来实现:字符串转整数,整数转字符串等操作;

#include<iostream>
#include<algorithm>
#include<sstream>
using namespace std;
int main(){
   
   
	//1.字符串转int 
	string s="0123";//我们需要把字符串s转化为int类型 
	int a;//用于存储之后转化的int值 
	stringstream ss;//定义一个变量名为ss的串流变量ss; 
	ss<<s;//简单理解为输入给ss 
	ss>>a; //简单理解为输出给a 
	printf("%d\n",a);
    //输出结果:123 
    
    //2.int类型转字符串
	string s1;
	int b=100; 
    ss.clear();//因为ss在上面已经使用过,所以需要清空再重复使用,ss.str("")效果相同 
	ss<<b;
	ss>>s1; 
    cout<<s1<<endl;
    //输出结果:100 
    return 0;
}

关于stringstream的用法还有很多,例如多个字符串拼接,字符串多组输入等等,有兴趣的同学可以自行查阅资料。

当然字符串与整数类型转化在c++11标准库中有函数可以直接使用!!

一、将各种类型整数(int,long long,double等等)通过to_string转为成string类型
std::string to_string(各种类型的value);

二、将string转换为各种整数类型的数据,例:
std::string str = “1000”;
int val = stoi(str);
long val = stol(str);
float val = stof(str);
long long val = stoll(str);`




#include<iostream>
using namespace std;
int main(){
   
   
	int a=1010;
	string s ;
	s=to_string(a); //int转string赋值到s	
    cout<<s<<endl;//输出1010;
    
    long long b;
    b=stoll(s);//string转long long赋值到b 
    printf("%lld\n",b);//输出1010;
    return 0;
}

5.Min/max函数

min,max函数可以方便地取两者中的较小值/较大值;包含在头文件:algorithm

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
   
   
	int ans=min(15,10);//max用法相同 
	printf("%d\n",ans);//输出10 
    return 0;
}

当然我们完全有能力自己写比较函数

#include<iostream>
using namespace std;
int min(int a,int b){
   
   return a<b?a:b;} 
int main(){
   
   
	int ans=min(15,10); 
	printf("%d\n",ans);//输出10 
    return 0;
}

6.赋初始值函数

初始化是写题时特别需要注意的,尤其是多组数据时;

首先是memset函数,头文件cstring
memset(data, 0, sizeof(data));

	int a[10000];
	memset(a,0,sizeof(a)); 
  //memset(a,-1,sizeof(a));

memset只能赋初值0和-1、true和false;而所有我们也需要赋其他值;
所以我们能可以用fill函数,头文件algorithm
fill(first,last,val);

	int a[10000];
    fill(a,a+100,233);//将数组a[0]到a[99]赋值为233 

7.__gcd()函数

最大公约数可以直接用algorithm库中的函数__gcd(x,y);

#include<iostream>
#include<algorithm>
using namespace std;
int gcd(int a,int b){
   
   return a%b==0?b:gcd(b,a%b);}//自写函数 
int main(){
   
   
	int a=24,b=16; 
	printf("%d\n",__gcd(a,b));//输出 8(库中函数)注意gcd前有两个下划线 ! 
	printf("%d\n",gcd(a,b));//输出 8  
    return 0;
}

标准库中还有许多可用的函数,大家有兴趣的也可以自己找百度、博客等地学习!!
第一次写博客,写得不好,见谅。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章