问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

C++中find()函数的使用方法详解

创作时间:
作者:
@小白创作中心

C++中find()函数的使用方法详解

引用
CSDN
1.
https://m.blog.csdn.net/2201_75539691/article/details/145100177

C++中find()函数是用于在字符串中查找指定的子串或单个字符,并返回子串/字符在字符串中的第一次出现位置。如果查找成功,则返回子串位置的值(起始位置);如果查找失败,则返回特殊值string::npos,表示没有找到。

find()是字符串类的成员函数,提供了多种不同调用模式:

size_t find(const string& str, size_t pos = 0) const; // 查找字符串
size_t find(const char* s, size_t pos = 0) const;     // 查找 C 风格字符串
size_t find(const char* s, size_t pos, size_t n) const; // 查找指定长度子串
size_t find(char c, size_t pos = 0) const;            // 查找单个字符
  • str:指定的查找子串;
  • s:C风格字符串,通过指针传递;
  • pos:开始查找的位置,默认为 0;
  • n:要匹配的子串长度;
  • c:单个字符。

如果在字符串中没有找到子串,find()函数将返回字符串类的静态常量npos,表示失败。npos是字符串类中定义的静态常量,应用于不实效的查找情况:

static const size_t npos = -1; // npos通常是一个特殊大的值,如:18446744073709551615

基本示例:字符串查找

本例用于查找字符串中指定子串的位置:

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s = "hello world hello everyone";
    string str = "llo";
    size_t n = s.find(str);   // 查找字符串 "llo",从字符串开始
    cout << n << endl;       // 输出:2
    n = s.find(str, n + 1);   // 从上次位置之后再查找
    cout << n << endl;       // 输出:14
    return 0;
}

字符查找

如果查找单个字符,可使用下列代码:

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s = "hello world hello everyone";
    size_t n = s.find('o');   // 查找字符 'o',从字符串开始
    cout << n << endl;       // 输出:4
    n = s.find('o', n + 1);   // 从上次位置之后再查找
    cout << n << endl;       // 输出:7
    return 0;
}

查找失败处理

通过判断返回值是否为npos,可以确实子串是否存在:

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s = "hello world hello everyone";
    string str = "bit";
    size_t n = s.find(str);
    if (n != string::npos) {
        cout << "找到,位置是:" << n << endl;
    } else {
        cout << "没有找到" << endl;
    }
    return 0;
}

应用提示

迭代查找子串

一些应用场景需要迭代找出所有匹配位置:

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s = "hello world hello everyone";
    string str = "llo";
    size_t pos = s.find(str);
    while (pos != string::npos) {
        cout << "找到:" << pos << endl;
        pos = s.find(str, pos + 1); // 继续查找
    }
    return 0;
}

提取上下文信息

find()函数还可以结合substr()提取匹配子串周围的上下文:

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s = "hello world hello everyone";
    string str = "world";
    size_t pos = s.find(str);
    if (pos != string::npos) {
        size_t start = (pos > 5) ? pos - 5 : 0;
        size_t len = str.length() + 10;
        cout << "上下文:" << s.substr(start, len) << endl;
    }
    return 0;
}

忽略大小写查找

find()函数默认区分大小写。如果需要忽略大小写,可以先将字符串转换为小写后再查找:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
    string s = "Hello World Hello Everyone";
    string str = "hello";
    // 转换为小写
    transform(s.begin(), s.end(), s.begin(), ::tolower);
    transform(str.begin(), str.end(), str.begin(), ::tolower);
    size_t pos = s.find(str);
    if (pos != string::npos) {
        cout << "找到,位置是:" << pos << endl;
    } else {
        cout << "没有找到" << endl;
    }
    return 0;
}

C++中的find()函数是字符串操作中极其重要的工具。通过对其多种使用方法的学习,我们可以解决多种字符串匹配问题:

  • 基本查找功能,用于快速定位子串或字符;
  • 通过npos判断查找失败,确保程序稳定性;
  • 结合循环,实现多次查找或全文匹配;
  • 配合substr()等函数,提取匹配上下文。

此外,在实际开发中,还可以通过忽略大小写等变形用法来满足不同场景需求。希望本文能够帮助读者深入理解并熟练掌握find()函数的使用。

本文原文来自CSDN

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号