分享一些實用的函數
我們的高級語言期末考試很快就要到了,在這裏給大家分享一些我所知道的好用的函數。
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;
}
標準庫中還有許多可用的函數,大家有興趣的也可以自己找百度、博客等地學習!!
第一次寫博客,寫得不好,見諒。。