ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-01-04 11:57:41  阅读:125  来源: 互联网

标签:std 入门 int 函数 c++ 实用 using include string


分享一些实用的函数

   我们的高级语言期末考试很快就要到了,在这里给大家分享一些我所知道的好用的函数,希望大家在期末考试中都能够取得一个好成绩!!

1. Sort函数

 void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
 包含在头文件algorithm内;
 (1)第一个参数*first*指起始地址;
 (2)第二个参数*last*指结束地址;
 (3)第三个参数*comp*指排序方法。如果第三个参数不写,则默认为从小到大排序

    用法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返回值是下标索引,所以用起来实用与方便。头文件为: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;我们可以简单地使用它来实现:字符串转int,int转字符串等操作;
#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的用法还有很多,例如多个字符串拼接,字符串多组输入等等,有兴趣的同学可以自行查阅资料
当然字符串与int类型转化在c++11标准库中有函数可以直接使用!!

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

二、将string转换为各种类型的数据:
std::string str = “1000”;
int val = std::stoi(str);
long val = std::stol(str);
float val = std::stof(str);
long long val = std::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;
}

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

标签:std,入门,int,函数,c++,实用,using,include,string
来源: https://blog.csdn.net/m0_52460083/article/details/112067285

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有