C++常见函数的基础算法

string 字符串

常用函数

  • substring()
  • string.length()&&string.size()
  • string.find()
  • string.replace()
  • string.substr()

string初始化和声明

#include<bits/stdc++.h>
using namespace std;
int main(){
string str1; //空字符串
string str2="hello, world"; //注意中间有空格和‘,’逗号
//通过str2赋值给str3
string str3=str2;
//初始化重复的字母
stirng str4(5,'C'); // str4="CCCCC"
return 0;
}

获取字符串的长度

string.length()&&string.size()

#include<bits/stdc++.h>
using namespace std;
int main(){
string str1="hello,, world";
int len=str1.length();
int len1=str1.size();
cout<<len<<len1; //输出13 包括2个逗号和一个空格
return 0;
}

length()size()返回的是无符号整数(0,1,2...) 使用时尽量用 (int)str1.length()进行强转

字符串查找

string.find()

#include<bits/stdc++.h>
using namespace std;
int main(){
string str1="hello,, world";
int x=str1.find(" world"); //用int 替代 size_t (无符合整数)
cout<<x; //输出7 空格的下标是7
int y=str1.find(" 2world");
cout<<y;//输出的-1;
}

找到相应的子串会返回子串中第一个char在字符串中的下标,如果没有找到会返回 -1

字符串拼接

+ || append()

#include<bits/stdc++.h>
using namespace std;
int main(){
string str1="hello";
string str2="world"
string str3(5,'Z');
// 用+进行拼接
string re1=str1+str2+str3;
cout<<re1; //输出helloworldZZZZZ
//用append()
string re2=str1.append(str2).append(str3).append("!");
cout<<re2; //输出helloworldZZZZZ!
}

**+ 拼接时可用char拼接,但在append()中的参数只能是string类型,不能是char **

string re1=str1+str2+str3+'!'+"2313";
cout<<re1;//输出helloworldZZZZZ!2313
string re2=str1.append(str2).append(str3).append('!');//append('!')会报错

字符串替换

string.replace()

#include<bits/stdc++.h>
using namespace std;
int main(){
string str1="hello,, world";
str1.replace(7,3,"333444");//7代表从下标为7的元素开始‘ ’,3代表要替换的长度(” wo“)
cout<<str1;//输出 hello,,333444rld
}

提取子字符串

substr()

#include<bits/stdc++.h>
using namespace std;
int main(){
string str1="hello,, world";
string re1=str1.substr(2,4); //2表示从下标2开始,4表示提取子字符串的长度;
cout<<re1;//输出 llo,
}

字符串比较

compare()

#include<bits/stdc++.h>
using namespace std;
int main(){
string str1="hello,,world";
string str2="heooll";
int in=str1.compare(str2); // 比较到下标为2时,'l'<'o' 所以返回-1
cout<<in;//输出-1 str1比str2小
// 用<,>,== 来比较字符串大小
int re1=str1<str2;
int re2=str1==str2;
int re3=str1>str2;
cout<<re1<<re2<<re3;//输出 1 0 0
}

字符串的比较不是越长就越大,比较时会从第一个元素进行一一对应的比较,如果有不相同的元素就会马上得出结果;

re=str1.compare(str2)

  • re==0 字符串相等
  • re<0 str1较小
  • re>0 str2较大

用 <,>,== 比较 re=str1<str2

  • re=1 str1小于str2
  • re=0 str1不小于str2

二分查找(库函数)

库函数只能对数组进行二分查找,而且必须为单调的

  • binary_search()
  • lower_bound()(常用)
  • upper_bound()

用于对已排序的序列(数组或容器)中查找特定的元素

#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int>nums={1,3,5,7,9};
int target=5;
//用binary_search()函数进行查找
bool found=binary_search(nums.begin(),nums.end(),target);
if (found){
cout<< "already found";
}else{
cout<<"not found!";
}
cout<<found;//输出1;
return 0;
}

该函数会返回一个bool类型的值

lower_bound()upper_bound()

使用前提:数组必须为非降序,即采用单调不减进行排序

lower_bound(st,ed,x)会返回地址[st,ed)中第一个大于等于x的元素的地址

upper_bound(st,ed,x)会返回地址[st,ed)中第一个大于x的元素的地址

如果不存在则返回最后一个元素的下一个位置,在vector中为end()的地址

#include<bits/stdc++.h>
using ll=long long;
using namespace std;
const ll N=5050;
int main() {
vector<ll> r;
ll n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
ll a;
cin >> a;
r.push_back(a);
}
vector<ll> x;
for (int j = 0; j < m; j++) {
ll b;
cin >> b;
x.push_back(b);
}
sort(r.begin(),r.end());//先对vector里面的元素进行排序
for(int i=0;i<m;i++) {
auto low = lower_bound(r.begin(), r.end(), x[i]);//用lower_bound返回要查找的元素位置的地址
if (low == r.end() || *(low) != x[i]) {
//如果low==r.end()说明未找到返回的是end()的地址或者未找到目标则会返回第一个大于x[i]的地址
cout << -1<<" " ;
continue;
}
cout << (low - r.begin())+1 <<" ";//用low的地址-r.begin()的地址=low的下标位置
}
return 0;
}

如果 x[i] 不存在于 r 中,lower_bound 将返回指向第一个大于 x[i] 的元素的迭代器(如果存在的话);如果所有元素都小于 x[i],则返回 r.end()

大写转小写(库函数)

  • islower()
  • isupper()
  • tolower()
  • toupper()

Ascii码

十进制 字符 十进制 字符 十进制 字符 十进制 字符
48 0 65-97 A-a 75-107 K-k 85-117 U-u
49 1 66-98 B-b 76-108 L-l 86-118 V-v
50 2 67-99 C-c 77-109 M-m 87-119 W-w
51 3 68-100 D-d 78-110 N-n 88-120 X-x
52 4 69-101 E-e 79-111 O-o 89-121 Y-y
53 5 70-102 F-f 80-112 P-p 90-122 Z-z
54 6 71-103 G-g 81-113 Q-q
55 7 72-104 H-h 82-114 R-r
56 8 73-105 I-i 83-115 S-s
57 9 74-106 J-j 84-116 T-t

利用Ascii码进行大小写互转

#include<bits/stdc++.h>
using namespace std;
int main(){
string s ;
cin>>s;
for(auto &i:s){
if (i>='A'&&i<='Z')i=i-'A'+'a';//大写转小写
else if (i>='a'&&i<='z')i=i-'a'+'A';//小写转大写
}
//或者
/*for(auto &i:s){
if (i>=65&&i<=90)i=i-'A'+'a';//大写转小写
else if (i>=97&&i<=122)i=i-'a'+'A';//小写转大写
} */
cout<<s;
return 0;
}

tolower()toupper()大小写互转

#include<bits/stdc++.h>
using namespace std;
char change(char x){
if(islower(x))x=toupper(x);//判断是小写 则转为大写
else if(isupper(x))x=tolower(x);//判断是大写 则转为小写
return x;
}
int main(){
string s ;
cin>>s;
for(auto &i:s)i=change(i);
cout<<s;
return 0;
}

islower(x)会返回bool类型

toupper(x)会将小写转换为大写,如果不是小写字母则不会执行任何操作

islower(),isupper(x)判断是否为小写或大写

#include<bits/stdc++.h>
using namespace std;
int main(){
char s ;
cin>>s;
if(islower(s))cout<<"是小写";
else if(isupper(s))cout<<"是大写";
cout<<s;
return 0;
}

排序(库函数)

sort()

sort()采用的是类似快排的算法,时间复杂度是O(nlogn),返回void

#include<bits/stdc++.h>
using namespace std;
//const N=1e5+5;
//int arr[N];
int main(){
int arr[]={7,4,8,1,9,3,5};
int size=sizeof(arr)/sizeof(int);
sort(arr,arr+size);
for(auto i:arr)cout<<i;
//或者用vector容器中的迭代器
vector<int>p={7,4,8,1,9,3,5};
sort(p.begin(),p.end()) ;
for(auto i:p)cout<<i;
return 0;
}

ps:sort()可以用第三个参数进行自定义比较,可传入一个函数,会一一的执行传入的函数

最值查找

  • min(),max()
  • min_element(),max_element()
  • nth_element()

min(),max()

max(x,y)和min()只能传入2个参数或者一个列表,返回void

#include<bits/stdc++.h>
using namespace std;
int main(){
//传入2个参数
cout<<max(4,5);//==5
//传入列表
cout<<min({1,23,4,5,});//==1
return 0;
}

max_element(),min_element()

max_element(st,ed),min_element(st,ed) 函数会返回最大或最小那个元素的地址(迭代器)

#include<bits/stdc++.h>
using namespace std;
int main(){
int a[]={1,23,4,5,6};
cout<<a[max_element(a,a+5)-a];//23
cout<<a[min_element(a,a+5)-a];//1
//也可用vector
vector<int>p={1,23,4,5,6};
cout<<p[max_element(p.begin(),p.end())-p.begin()];//23
return 0;
}

nth_element()

进行部分排序,返回void

#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int>p={1,4,2,6,7,8};
nth_element(p.begin(),p.begin()+2,p.end());//对[p.begin(),p.begin()+2]范围内排序
for(auto i:p)cout<<i<<" ";//输出2 1 4 6 7 8
return 0;
}

全排列(库函数)

next_permutation()

prev_permutation()

next_permutation()

next_permutation(st,ed)用于生成当前序列的下一个序列,如果存在下一个序列则会将当前序列更改为下一个序列,并且返回true,如果不存在则会将当前的序列更改为第一个序列,并且返回false,一般第一序列是最小序列

#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int>p={1,4,2,3};
sort(p.begin(),p.end());//一般先进行排序,使其为最小序列 1234
next_permutation(p.begin(),p.end());//排一次
for(auto i:p)cout<<i<<" ";//输出1243
return 0;
}

输出全排列

#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int>p={1,3,2};
sort(p.begin(),p.end());//排序 1 2 3
for(auto i:p)cout<<i<<" ";
cout<<endl;
while(next_permutation(p.begin(),p.end())){
for(auto i:p)cout<<i<<" ";
cout<<endl;
}
/*输出
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1*/
for(auto i:p)cout<<i<<" ";//最后序列会回到一个序列,输出123
return 0;
}

prev_permutation()

prev_permutation(st,ed)用于生成当前序列的上一个序列,如果存在上一个序列则会将当前序列更改为上一个序列,并且返回true,如果不存在则会将当前的序列更改为第最后个序列,并且返回false****,一般第一序列是最大序列

#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int>p={1,3,2};
sort(p.begin(),p.end());//排序 123
reverse(p.begin(),p.end());//逆转为321 使第一个序列为最大序列
for(auto i:p)cout<<i<<" ";
cout<<endl;
while(prev_permutation(p.begin(),p.end())){
for(auto i:p)cout<<i<<" ";
cout<<endl;
}
for(auto i:p)cout<<i<<" ";//最后的序列会变回为第一个序列 即最大的序列
/*
3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3
3 2 1*/
return 0;
}

其他库函数

  • memset()
  • swap()
  • reverse()
  • unique()

memset()

一般用于初始化数组中的元素值

#include<bits/stdc++.h>
using namespace std;
int main(){
int a[10];
//将数组a初始化为0;
memset(a,0,sizeof a);
for(auto i:a)cout<<i;
return 0;
}

swap()

用于交换2个变量的值,返回void

#include<bits/stdc++.h>
using namespace std;
int main(){
int a=2,b=4;
swap(a,b);//交换a,b a=4,b=2;
cout<<a<<b;
return 0;
}

reverse()

reverse(st,ed)用于数组或vector容器中的元素逆转

#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int>p={1,2,3,4};
reverse(p.begin(),p.end());//对p中的元素进行逆转
for(auto i:p)cout<<i;
//输出4321
return 0;
}

unique()

unique(st,ed)函数用于对[st,ed)中相邻元素去重,!!!并且会把重复的元素放回去重元素的后面,然后返回重复元素中第一个元素的地址,由于只会对相邻元素进行去重,所以要想达到完全去重,使用前需要先排序,可以结合erase()达到完整去重效果

#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int>p={1,2,3,3,4,4,2,1};
sort(p.begin(),p.end());
auto gap=unique(p.begin() ,p.end());
//用erase
p.erase(gap,p.end());
for(auto i:p)cout<<i;//1234
//用for循环
for(int i=0;i<gap-p.begin();i++) cout<<p[i];//1234
return 0;
}
posted @   zpa666  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示