C++ string中的find() 函数

C++ string中的find() 函数,第1张

1、string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos,也就是-1。(返回值可以看成是一个int型的数)

例子:找到的情况

#include
#include

using namespace std;

int main()
{
   string str = "aabc";
   int pos = str.find("abc");    
   if (pos != str.npos)
   {
      cout << pos <

例子:找不到的情况,调试可以看到npos的值为-1,即是4294967295

#include
#include

using namespace std;

int main()
{
   string str = "aabc";
   int pos = str.find("d");    
   if (pos != str.npos)
   {
      cout << pos <

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1324433.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-12
下一篇2022-06-12

发表评论

登录后才能评论

评论列表(0条)