一、常用字符串函数
1、strcmp() 函数
头文件:#include <cstring>
功能: 比较两个字符串
一般形式: strcmp(字符串1,字符串2)
规则:
- 对两个字符串自左至右逐个字符相比(按ASCII码值大小比较),直到出现不同的字符或遇到‘\0’为止。
- 如果全部字符相同,则认为相等;若出现不相同的字符,则以第一个不相同的字符的比较结果为准。
- 如果两个字符串都由英文字母组成,则有一个简单的规律:在英文字典中位置在后面的为“大”,还要特别注意:小写字母 > 大写字母 > 数字。
- 返回值:
(1)字符串1=字符串2,返回0。
(2)字符串1>字符串2,返回一个正整数(不是1)。
(3)字符串1<字符串2,返回一个负整数(不是-1)。
2、reverse()函数
头文件:#include <algorithm>
功能: 可以对字符串进行反转操作
注意: 容器类型的要用begin()和end()来指定反转的区域
数组类型的直接用int类型即可
用法:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
string s="hello";
reverse(s.begin(),s.end());
char c[]="hello";
reverse(c,c+strlen(c));
char x[6]={'h','e','l','l','o'};
reverse(x,x+strlen(x));
char str[11];
for(int i=0;i<5;i++)cin>>str[i];
reverse(str,str+5);
cout<<s<<endl;
cout<<c<<endl;
cout<<x<<endl;
for(int i=0;i<5;i++)cout<<str[i];
return 0;
}
结果如下:
3、substr()函数
头文件:#include <cstring>
功能: 可以提取string字符串中的相应子字符串
说明: 该函数有两个参数,第一个参数为需要提取的子字符串的起始下标,
第二个参数是需要提取的子字符串的长度。
用法:
#include <iostream>
#include <string>
using namespace std;
int main(){
string s1 = "first second third";
string s2;
s2 = s1.substr(6, 6);
cout<< s1 <<endl;
cout<< s2 <<endl;
return 0;
}
结果如下:
4、find() 函数
-
头文件:#include <cstring>
-
功能: 在字符串从左往右开始寻找指定子串,存在的话返回子串在字符串中的
位置下标,不存在返回 s.npos -
一般形式: s.find(‘子串’)
参考样例:
//1、从字符串b的下标0开始找,也就是找到第一个子串a所在的位置,
//返回的是子串第一个字母所在的下标
string a,b;
getline(a);
getline(b);
int p=b.find(a);
if(p==b.npos) //找不到
cout<<"a不是b的字串"<<endl;
else //找到了
cout<<"a出现的下标是"<<p<<endl;
//2、从指定位置之后查找,可以用于查找第i次出现的子串
int f;
p=b.find(a,f);//从b的下标为f开始查找,即第f+1个字母
//3、统计b中所有a出现的次数
cnt=0;//计数
p=s2.find(s1);
while(p!=s2.npos){
cnt++;
p=s2.find(s1,p+1);//每次只往后推一个来找,而不是直接跳到子串结束的位置(p+a.size())
} //可以不漏下子串重叠的情况,比如ababa的字串aba
cout<<cnt;
//4、rfind() :b.rfind(a,f)表示从左往右下标为 f的字符(包括下标 f)开始往前找,
//即第f+1个字符开始找。以下面为例时,当f=5,注意输出的是4,即 b字符串中第二个a子串,
//这个 dsf的 f并不在前6个字母之中(f=5是下标,第6个字母),所以这个f只针对字串首
//而不针对字串尾。只要子串头在限定范围内,那就可以找到。
string a="dsf";
string b="adsfdsfadsf";
int p=b.rfind(a);
p=b.rfind(a,5);
cout<<p;//p=4
p=b.rfind(a,6);
cout<<p;//p=4
p=b.rfind(a,7);
cout<<p;//p=4
p=b.rfind(a,8);
cout<<p;//p=8
p=b.rfind(a,9);
cout<<p;//p=8
p=b.rfind(a,10);
cout<<p;//p=8
//5、find_first_of():查找第一个属于b并且属于a的字母。只针对字母了。
//也是可以设置开始位置的。
string a="dsf";
string b="adsfdsfadsf";
cout<<b.find_first_of(a);//输出1,b[0]='a'不属于a串,但b[1]=d,在a中是有的
cout<<b.find_first_of(a,7);//输出8,b[7]='a',b[8]='d'在a中有出现
//6、find_last_of():从后往前找第一个属于b并且属于a的字母。支持设置开始位置。
//7、find_first_not_of():查找第一个属于b而不属于a的字母。支持设置开始位置。
//8、find_last_not_of():从后往前找,查找第一个属于b而不属于a的字母。
//支持设置开始位置。
3、to_string()函数
-
头文件:#include <cstring>
-
功能: 将数字常量转换为字符串,返回值为转换完毕的字符串
-
说明: std::to_string是C++标准(2011年)的最新版本中引入的功能。旧的编译器可能不支持它。
-
一般形式:
string s = to_string(i); //将整数i转换为字符串表示形式
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
参考样例:
#include <iostream>
#include <string>
using namespace std;
int main(){
string pi = "pi is " + std::to_string(3.1415926);
string perfect = to_string(1 + 2 + 4 + 7 + 14) + " is a perfect number";
cout << pi << '\n';
cout << perfect << '\n';
system("pause");
return 0;
}
二、常用字符函数
1、isdigit() 函数
头文件:(C)#include <ctype.h> (C++) #include <cctype>
作用: 判断字符是否为数字,若是则返回true,否则返回false
2、isalpha() 函数
头文件:(C)#include <ctype.h> (C++) #include <cctype>
作用: 判断字符是否为字母,若是则返回true,否则返回false
3、isupper() 函数
头文件:(C)#include <ctype.h> (C++) #include <cctype>
作用: 判断字符是否为大写字母,若是则返回true,否则返回false
4、islower() 函数
头文件:(C)#include <ctype.h> (C++) #include <cctype>
作用: 判断字符是否为小写字母,若是则返回true,否则返回false
5、tolower() 函数
头文件:(C)#include <ctype.h> (C++)#include <cctype>
作用: 将大写字母转化为小写字母
6、toupper() 函数
头文件:(C)#include <ctype.h> (C++)#include <cctype>
作用: 小写字母转化为大写字母
经过测试,如果不包含头文件< cctype>仅仅有< iostream>也是可以的
三、常用通用算法函数
1、unique() 函数
头文件:#include <algorithm>
功能: unique函数可以删除有序数组中的重复元素(只保留一个)
即把重复的元素移到后面,依然保存在原数组。
注意: 使用前需要对数组进行排序
用法1: 对于长度为n数组a,unique(a,a+n) - a返回的是去重后的数组长度,即l
#include<cstdio>
#include<algorithm>
using namespace std;
int a[50005];
int main()
{
int n;
while(scanf("%d",&n)!=-1)
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
int l=unique(a,a+n)-a;
for(int i=0;i<l;i++)
printf("%d ",a[i]);
}
return 0;
}
若把整个数组输出,结果如下:
其中 1 2 8 9 10就是去重后的数组
用法2: 利用vector把后面藏着的元素删除
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 1000;
int a[N + 5];
int main()
{
int n;
while (cin >> n)
{
for (int i = 0;i < n;++i) scanf("%d",&a[i]);
sort (a, a + n);
vector<int>v (a, a + n);
vector<int>::iterator it = unique (v.begin(), v.end() );
v.erase (it, v.end() );//这里就是把后面藏起来的重复元素删除了
for ( it = v.begin() ; it != v.end() ; it++ )
{
printf ("%d ", *it);
}
puts("");
}
return 0;
}
输出结果如下:
2、lower_bound() 函数
头文件:#include <algorithm>
用法1: lower_bound(begin, end, num)
功能: 从有序(从小到大)数组的 begin 位置到 end-1 位置二分查找第一个大于或等于num 的数字,找到返回该数字的地址,不存在则返回 end 。通过返回的地址减去起始地址 begin,得到该数字在数组中的下标。
用法2: lower_bound( begin, end, num, greater<type>() )
功能: 从有序(从大到小)数组的 begin 位置到 end-1 位置二分查找第一个小于或等于 num 的数字,找到返回该数字的地址,不存在则返回 end。通过返回的地址减去起始地址 begin,得到该数字在数组中的下标。
3、upper_bound() 函数
头文件:#include <algorithm>
用法1: upper_bound( begin,end,num)
功能: 从有序(从小到大)数组的 begin 位置到 end-1 位置二分查找第一个大于 num 的数字,找到返回该数字的地址,不存在则返回 end。通过返回的地址减去起始地址 begin,得到该数字在数组中的下标。
用法2: upper_bound( begin,end,num,greater<type>() )
功能: 从有序(从大到小)数组的 begin 位置到 end-1 位置二分查找第一个小于 num 的数字,找到返回该数字的地址,不存在则返回 end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
代码如下:
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+10;
const int INF=2*int(1e9)+10;
#define LL long long
int cmd(int a,int b){
return a>b;
}
int main(){
int num[6]={1,2,4,7,15,34};
sort(num,num+6); //按从小到大排序
int pos1=lower_bound(num,num+6,7)-num; //返回数组中第一个大于或等于被查数的值
int pos2=upper_bound(num,num+6,7)-num; //返回数组中第一个大于被查数的值
cout<<pos1<<" "<<num[pos1]<<endl;
cout<<pos2<<" "<<num[pos2]<<endl;
sort(num,num+6,cmd); //按从大到小排序
int pos3=lower_bound(num,num+6,7,greater<int>())-num; //返回数组中第一个小于或等于被查数的值
int pos4=upper_bound(num,num+6,7,greater<int>())-num; //返回数组中第一个小于被查数的值
cout<<pos3<<" "<<num[pos3]<<endl;
cout<<pos4<<" "<<num[pos4]<<endl;
return 0;
}
四、常用数学函数
1、__gcd() 函数
头文件:(C++) #include <algorithm>
作用: 求两个数的最大公约数,int、long long类型都可以,需要注意的是两个类型必须要相同,以及不能用浮点型。
2、accumulate() 函数
头文件:(C++) #include <numeric>
作用: 一个是累加求和,另一个是自定义类型数据的处理