【C++】C++中find_first_of函数解析

目录

一.函数介绍

二.函数原型

三.函数示例


一.函数介绍

在C++中, find_first_of  是  std::string  类的一个成员函数,用于在字符串中查找第一次出现的任何字符(或字符序列)的索引位置。这个函数可以接收单个字符或字符序列作为参数,并返回第一个匹配项的起始位置。

二.函数原型

以下是  find_first_of  的一些基本用法:

size_t find_first_of(charT ch, size_t pos = 0) const;
size_t find_first_of(const charT* s, size_t pos = 0) const;
size_t find_first_of(const string& str, size_t pos = 0) const;
size_t find_first_of(const charT* s, size_t pos, size_t n) const;

参数说明

 ch :要查找的单个字符。

 s :要查找的字符序列。

 pos :开始查找的位置(默认为0)。

 str :要查找的字符串。

 n :字符序列的长度。

返回值

返回第一次出现的字符或字符序列的起始位置的索引。如果没有找到,则返回  string::npos 。

示例代码

#include <iostream>
#include <string>


int main() {
    std::string str = "Hello World!";
    std::string chars = "Wo";

    size_t pos = str.find_first_of(chars);
    if (pos != std::string::npos) {
        std::cout << "First occurrence of any character in '" << chars << "' is at position: " << pos << std::endl;
    } else {
        std::cout << "No occurrence of any character in '" << chars << "' found." << std::endl;
    }

    return 0;
}

输出

First occurrence of any character in 'Wo' is at position: 7

在这个例子中, find_first_of  查找字符串  "Hello World!"  中第一次出现的字符  "W"  或  "o"  的位置。结果是  "W"  在位置7首次出现。

三.函数示例

std::string str = "Hello World!";
std::string chars = "abc";

size_t pos = str.find_first_of(chars, 5);  // 从位置5开始查找
if (pos != std::string::npos) {
    std::cout << "First occurrence of any character in '" << chars << "' starting from position 5 is at position: " << pos << std::endl;
} else {
    std::cout << "No occurrence of any character in '" << chars << "' found starting from position 5." << std::endl;
}

在这个例子中,查找从位置5开始,因此不会考虑  "Hello"  中的  "a" 。

 find_first_of  是处理字符串查找任务时非常有用的函数之一。

相关推荐

  1. opencv | opencvblobFromImage函数

    2024-07-19 17:10:02       38 阅读
  2. 【C++】C++find_first_of函数

    2024-07-19 17:10:02       21 阅读
  3. Linux cp命令(cp指令)

    2024-07-19 17:10:02       46 阅读
  4. Python Lambda函数

    2024-07-19 17:10:02       57 阅读
  5. Hive之函数

    2024-07-19 17:10:02       41 阅读
  6. SparkSQL之函数

    2024-07-19 17:10:02       46 阅读
  7. dify-on-wechat涉及企业微信几个函数

    2024-07-19 17:10:02       24 阅读
  8. transforms.Compose()函数作用

    2024-07-19 17:10:02       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 17:10:02       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 17:10:02       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 17:10:02       62 阅读
  4. Python语言-面向对象

    2024-07-19 17:10:02       72 阅读

热门阅读

  1. PHP MySQL 读取数据

    2024-07-19 17:10:02       19 阅读
  2. Handler续谈(epoll)

    2024-07-19 17:10:02       19 阅读
  3. Git提交到错误分支怎么办?(解决办法)

    2024-07-19 17:10:02       22 阅读
  4. 在ubuntu系统上安装qt 2

    2024-07-19 17:10:02       20 阅读
  5. CONFIG_MTD_SPI_NOR_USE_4K_SECTORS

    2024-07-19 17:10:02       23 阅读
  6. 网络通信协议

    2024-07-19 17:10:02       14 阅读
  7. opencv 使用XML和YAML格式来输入输出文件

    2024-07-19 17:10:02       26 阅读
  8. css2024

    2024-07-19 17:10:02       21 阅读
  9. Jangow

    Jangow

    2024-07-19 17:10:02      23 阅读
  10. new一个对象的具体步骤

    2024-07-19 17:10:02       24 阅读
  11. Hive 的 classpath 简介

    2024-07-19 17:10:02       22 阅读