讨论 C++ strings 的 find 方法及 npos 常量
发布时间
阅读量:
阅读量
find()函数功能解析
find()函数的功能在于检索字符串中是否存在特定的子串。
一旦发现匹配内容,将返回该子串首次出现位置的起始索引值。
若未检索到对应内容,则系统将返回数值-1。
基础示例:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv) {
string s="aabbcc";
int index1=s.find("ddd");
int index2=s.find("bb");
cout<<index1<<endl; //不包含子串,输出-1
cout<<index2<<endl; //包含子串,输出2
return 0;
}
string::npos的定义与应用
npos 被定义为一个固定数值,用于标识无效的位置,其标准取值为-1。然而,该数值的数据类型并非整型,而是 str
全部评论 (0)
还没有任何评论哟~
