C++ string 找出字符第n次出现的位置
2021/5/7 22:27:01
本文主要是介绍C++ string 找出字符第n次出现的位置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include#include#includeusing namespace std;/* T:要搜索的串 P:要匹配的串 */int pos(string T, string P, int n){ if(n==0)return -1;//相当于没找 int count = 0; unsigned begined = 0; while((begined=T.find(P,begined))!=string::npos){ count++; begined += P.length(); if(n==count){ return begined-1; } } return -2;//意味着没有那么多次}int main(){ int n = 2; string str1 = "mamad"; string str2 = "m"; cout<<str2<<" 第 "<<n<<" 次出现的位置是 "<<pos(str1, str2, n)<<endl; return 0;}
这篇关于C++ string 找出字符第n次出现的位置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-05feign默认connecttimeout和readtimeout是多少-icode9专业技术文章分享
- 2024-07-05idea控制台,日志太多,导致部分想看得日志被刷走 搜不到-icode9专业技术文章分享
- 2024-07-05The server selected protocol version Tls10 is not accepted by client preferences [TLs12]-icode9专业技术文章分享
- 2024-07-05怎么清理项目缓存-icode9专业技术文章分享
- 2024-07-04安装 Eyoucms详细图文教程-icode9专业技术文章分享
- 2024-07-04ueditor 复制文章时,图片的链接是一个下载图片地址,该如何处理?-icode9专业技术文章分享
- 2024-07-04怎样判断host有没有对wordpress有缓存呢-icode9专业技术文章分享
- 2024-07-04具有编译功能的系统make后,无法ssh连接-icode9专业技术文章分享
- 2024-07-04make后如何升级ssh-icode9专业技术文章分享
- 2024-07-03微信支付提示下单账户与支付账户不一致-icode9专业技术文章分享