find函数的运用

find函数就像是一个"查找小助手",它能在一串数据(比如字符串、数组)中帮你找到特定元素的位置。

#include <iostream>
#include <string> // 必须包含这个头文件
using namespace std;
int main() {
    // 创建一个字符串
    std::string name = "LiuYouYi"; // 你的名字
    // 使用find查找字母'Y'的位置
    // find(要找的字符, 从哪里开始找)
    char a='Y';
    //cin>> a;
    int position = name.find(a, 0); 
    //如果找不到 返回-1 
    //cout<<position<<endl;    
    // 检查是否找到
    if (position >=0) {
        std::cout << "找到了!"<<a<<"在第:" << position+1 <<"个位置." <<std::endl;
        std::cout << "验证:" << name[position] << std::endl;
    } else {
        std::cout << "没找到!" << std::endl;
    }
    
    return 0;
}

1.name.find('Y', 0) 从位置0开始找字母'Y'

2.字符串位置从0开始:L(0)-i(1)-u(2)-Y(3)-o(4)-u(5)-Y(6)-i(7)第一个'Y'在位置3

3.npos 是一个特殊值,表示"没找到"

在数组中查找数字

#include <iostream>
#include <algorithm> // 包含find函数
#include <vector>    // 使用vector动态数组

int main() {
    // 创建包含你数学成绩的数组
    std::vector<int> scores = {85, 92, 78, 95, 88};
    
    // 使用find查找92分的位置
    // find(开始位置, 结束位置, 要找的值)
    auto it = std::find(scores.begin(), scores.end(), 92);
    
    // 检查是否找到
    if (it != scores.end()) {
        // 计算位置(从0开始)
        int position = std::distance(scores.begin(), it);
        std::cout << "找到92分了!位置在:" << position << std::endl;
        std::cout << "验证:第" << position+1 << "个成绩是:" << *it << std::endl;
    } else {
        std::cout << "没找到92分" << std::endl;
    }
    
    return 0;
}

1.scores.begin() - 数组开始位置

2.scores.end() - 数组结束位置(最后一个元素后面)

3.std::distance 计算两个位置之间的距离(索引值)

#include <iostream>
#include <string>

int main() {
    std::string sentence = "LiuYouYi is learning C++ find function!";
    
    // 查找子字符串"learning"
    size_t pos = sentence.find("learning", 0);
    
    if (pos != std::string::npos) {
        std::cout << "找到'learning'了!起始位置:" << pos << std::endl;
        
        // 提取找到的部分
        std::string found = sentence.substr(pos, 8); // 从pos开始取8个字符
        std::cout << "提取的部分:" << found << std::endl;
    } else {
        std::cout << "没找到'learning'" << std::endl;
    }
    
    return 0;
}

1.可以查找整个字符串而不仅是单个字符

2.substr 函数可以从字符串中提取一部分

#include <iostream>
#include <string>

int main() {
    std::string text = "C++ is cool, C++ is powerful, C++ is fun!";
    std::string target = "C++";
    size_t pos = 0;
    int count = 0;
    
    std::cout << "在文本中找 '" << target << "':" << std::endl;
    
    // 循环查找所有匹配
    while ((pos = text.find(target, pos)) != std::string::npos) {
        std::cout << "" << ++count << "个在位置:" << pos << std::endl;
        pos++; // 移动到下一个位置继续查找
    }
    
    if (count == 0) {
        std::cout << "没找到!" << std::endl;
    } else {
        std::cout << "总共找到 " << count << "" << std::endl;
    }
    
    return 0;
}

1.使用循环查找所有匹配项

2.每次找到后,从下一个位置继续查找

3.统计找到的总次数

查找类型      使用场景            代码示例

查找字符      在字符串中找单个字母      str.find('A', 0)

查找数字      在数组中找特定值        find(arr.begin(), arr.end(), 5)

查找子串      在长文本中找单词        str.find("hello", 0)

查找全部      找出所有匹配项         while((pos=find(...))!=npos)

常见错误及解决

1.忘记包含头文件

#include <string>   // 字符串操作
#include <algorithm> // 数组/容器操作

2.忘记检查是否找到

// 错误:直接使用位置
size_t pos = str.find('X');
std::cout << str.substr(pos); // 如果没找到会出错

// 正确:先检查
if (pos != std::string::npos) {
    // 使用位置
}

2.位置计数错误

记住:位置从0开始

第一个字符位置是0,第二个是1,依此类推

练习挑战

试着写一个程序:

1.创建一个字符串:"Hello, my name is LiuYouYi"

2.查找你的姓"Liu"的位置

3.查找名字"YouYi"的位置

4.计算字符串中字母'i'出现的次数

#include <iostream>
#include <string> // 必须包含这个头文件
using namespace std;
int main() {
    // 创建一个字符串
    std::string name = "LiuYouYiWANGCHICHIYA"; // 你的名字
    // 使用find查找字母'Y'的位置
    // find(要找的字符, 从哪里开始找)
    char a='Y';
    //cin>> a;
    int position = name.find(a, 0); 
    int i=0;
    while(position >=0){
        i++;
        cout << "找到了!"<<a<<"在第:" << position+1 <<"个位置." <<std::endl;
        cout << "验证:" << name[position] << std::endl;
        position = name.find(a, position+1); 
    }
    cout<<"字母"<<a<<""<<name<<"出现了:"<<i<<""; 
    return 0;
}

 

posted @ 2025-05-30 20:32  爱吃泡面的皮卡  阅读(27)  评论(0)    收藏  举报