【C++】find() 函数全解

devtools/2025/1/13 6:10:20/
class="tags" href="/C.html" title=c>cle class="tags" href="/C.html" title=c>class="baidu_pl">
class="tags" href="/C.html" title=c>cle_class="tags" href="/C.html" title=c>content" class="tags" href="/C.html" title=c>class="articlass="tags" href="/C.html" title=c>cle_class="tags" href="/C.html" title=c>content class="tags" href="/C.html" title=c>clearfix">
class="tags" href="/C.html" title=c>content_views" class="tags" href="/C.html" title=c>class="markdown_views prism-tomorrow-night">class="tags" href="/C.html" title=c>cap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-bloclass="tags" href="/C.html" title=c>ck" style="-webkit-tap-highlight-class="tags" href="/C.html" title=c>color: rgba(0, 0, 0, 0);">

class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/class="tags" href="/C.html" title=c>cb7f73class="tags" href="/C.html" title=c>cda1a14130840e356class="tags" href="/C.html" title=c>cb0b6f9e4.png#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" width="100" />

<class="tags" href="/C.html" title=c>center> 博客主页: [小ᶻ☡꙳ᵃⁱᵍᶜ꙳] class="tags" href="/C.html" title=c>center> <class="tags" href="/C.html" title=c>center> 本文专栏: C++ class="tags" href="/C.html" title=c>center>

class="tags" href="/C.html" title=c>class="toclass="tags" href="/C.html" title=c>c">

文章目录

  • 💯前言
  • 💯一、`find()` 函数概述
    • 1.基本作用
    • 2.函数形式解释
    • 3.举例:查找失败
  • 💯二、解析和示例解释
    • 1. 基本示例:字符串查找
    • 2. 字符查找
    • 3. 查找失败处理
  • 💯三、应用提示
    • 1. 迭代查找子串
    • 2. 提取上下文信息
    • 3. 忽略大小写查找
  • 💯四、小结


class="tags" href="/C.html" title=c>c="https://img-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/00d4fdeb7b0d4dbb99e88f325ef249d8.gif#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" width="1000" height="100" />


💯前言

  • C++计算机软件开发中࿰class="tags" href="/C.html" title=c>c;对字符串进行操作是常见的需求。字符串里的查询功能是基础࿰class="tags" href="/C.html" title=c>c;也是高性能编程中的重要一环。C++ 中提供了完善的字符串操作函数࿰class="tags" href="/C.html" title=c>c;而其中的 <class="tags" href="/C.html" title=c>code>find()class="tags" href="/C.html" title=c>code> 函数࿰class="tags" href="/C.html" title=c>c;是用于在字符串中查找子串或字符的工具。
    本文将全面解析 C++ 中的 <class="tags" href="/C.html" title=c>code>find()class="tags" href="/C.html" title=c>code> 函数࿰class="tags" href="/C.html" title=c>c;包括基础解释࿰class="tags" href="/C.html" title=c>c;示例解析࿰class="tags" href="/C.html" title=c>c;以及实际应用中的特别经验。我们将重点解析不同功能开发情景中的实现࿰class="tags" href="/C.html" title=c>c;并提供简单明显的实验代码。
    C++ 参考手册
    class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/f0db7class="tags" href="/C.html" title=c>c348331440fa266b3class="tags" href="/C.html" title=c>c24f558694.png#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" />

💯一、<class="tags" href="/C.html" title=c>code>find()class="tags" href="/C.html" title=c>code> 函数概述

1.基本作用

C++ 中的 <class="tags" href="/C.html" title=c>code>find()class="tags" href="/C.html" title=c>code> 函数是用于在字符串中查找指定的子串或单个字符࿰class="tags" href="/C.html" title=c>c;并返回子串/字符在字符串中的第一次出现位置。

  • 如果查找成功࿰class="tags" href="/C.html" title=c>c;则返回子串位置的值(起始位置);
  • 如果查找失败࿰class="tags" href="/C.html" title=c>c;则返回特殊值 <class="tags" href="/C.html" title=c>code>string::nposclass="tags" href="/C.html" title=c>code>࿰class="tags" href="/C.html" title=c>c;表示没有找到。

2.函数形式解释

<class="tags" href="/C.html" title=c>code>find()class="tags" href="/C.html" title=c>code> 是字符串类的成员函数࿰class="tags" href="/C.html" title=c>c;提供了多种不同调用模式:

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">size_t class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">findclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>const stringclass="tags" href="/C.html" title=c>class="token operator">& strclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, size_t pos class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>constclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 查找字符串
size_t class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">findclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>const class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>charclass="tags" href="/C.html" title=c>class="token operator">* sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, size_t pos class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>constclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;     class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 查找 C 风格字符串
size_t class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">findclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>const class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>charclass="tags" href="/C.html" title=c>class="token operator">* sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, size_t posclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, size_t nclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>constclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 查找指定长度子串
size_t class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">findclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>char class="tags" href="/C.html" title=c>cclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, size_t pos class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>constclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;            class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 查找单个字符
class="tags" href="/C.html" title=c>code>
  • <class="tags" href="/C.html" title=c>code>strclass="tags" href="/C.html" title=c>code> :指定的查找子串;
  • <class="tags" href="/C.html" title=c>code>sclass="tags" href="/C.html" title=c>code> : C 风格字符串࿰class="tags" href="/C.html" title=c>c;通过指针传递;
  • <class="tags" href="/C.html" title=c>code>posclass="tags" href="/C.html" title=c>code> :开始查找的位置࿰class="tags" href="/C.html" title=c>c;默认为 0;
  • <class="tags" href="/C.html" title=c>code>nclass="tags" href="/C.html" title=c>code> :要匹配的子串长度;
  • <class="tags" href="/C.html" title=c>code>class="tags" href="/C.html" title=c>cclass="tags" href="/C.html" title=c>code> :单个字符。

3.举例:查找失败

如果在字符串中没有找到子串࿰class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>find()class="tags" href="/C.html" title=c>code> 函数将返回字符串类的静态常量 <class="tags" href="/C.html" title=c>code>nposclass="tags" href="/C.html" title=c>code> ࿰class="tags" href="/C.html" title=c>c;表示失败。

<class="tags" href="/C.html" title=c>code>nposclass="tags" href="/C.html" title=c>code> 是字符串类中定义的静态常量࿰class="tags" href="/C.html" title=c>c;应用于不实效的查找情况:

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token keyword">staticlass="tags" href="/C.html" title=c>c class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>const size_t npos class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token operator">-class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// npos通常是一个特殊大的值࿰class="tags" href="/C.html" title=c>c;如:18446744073709551615
class="tags" href="/C.html" title=c>code>

实例:

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><iostream>
class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><string>
class="tags" href="/C.html" title=c>class="token keyword">using class="tags" href="/C.html" title=c>class="token keyword">namespaclass="tags" href="/C.html" title=c>ce stdclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{string s class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token string">"hello world"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;size_t n class="tags" href="/C.html" title=c>class="token operator">= sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">findclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token string">"bit"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// "bit" 不存在࿰class="tags" href="/C.html" title=c>c;回调 nposclass="tags" href="/C.html" title=c>class="token keyword">if class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(n class="tags" href="/C.html" title=c>class="token operator">== stringclass="tags" href="/C.html" title=c>class="token double-class="tags" href="/C.html" title=c>colon punclass="tags" href="/C.html" title=c>ctuation">::nposclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token string">"查找失败" class="tags" href="/C.html" title=c>class="token operator"><< endlclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">} class="tags" href="/C.html" title=c>class="token keyword">else class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token string">"找到࿰class="tags" href="/C.html" title=c>c;位置是:" class="tags" href="/C.html" title=c>class="token operator"><< n class="tags" href="/C.html" title=c>class="token operator"><< endlclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>

输出:
<class="tags" href="/C.html" title=c>code>查找失败class="tags" href="/C.html" title=c>code>


💯二、解析和示例解释

1. 基本示例:字符串查找

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

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><iostream>
class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><string>
class="tags" href="/C.html" title=c>class="token keyword">using class="tags" href="/C.html" title=c>class="token keyword">namespaclass="tags" href="/C.html" title=c>ce stdclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{string s class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token string">"hello world hello everyone"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;string str class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token string">"llo"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;size_t n class="tags" href="/C.html" title=c>class="token operator">= sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">findclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(strclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;   class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 查找字符串 "llo"࿰class="tags" href="/C.html" title=c>c;从字符串开始class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< n class="tags" href="/C.html" title=c>class="token operator"><< endlclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;       class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 输出:2n class="tags" href="/C.html" title=c>class="token operator">= sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">findclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(strclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, n class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;   class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 从上次位置之后再查找class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< n class="tags" href="/C.html" title=c>class="token operator"><< endlclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;       class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 输出:14class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>

解释:

  • <class="tags" href="/C.html" title=c>code>find()class="tags" href="/C.html" title=c>code> 实现了查找子串的功能࿰class="tags" href="/C.html" title=c>c;并且返回第一次匹配的位置。
  • 完成一次查找后࿰class="tags" href="/C.html" title=c>c;通过 <class="tags" href="/C.html" title=c>code>n + 1class="tags" href="/C.html" title=c>code> 跳过已查找部分࿰class="tags" href="/C.html" title=c>c;继续找到下一个第一匹配。

2. 字符查找

如果查找单个字符࿰class="tags" href="/C.html" title=c>c;可使用下列代码:

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><iostream>
class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><string>
class="tags" href="/C.html" title=c>class="token keyword">using class="tags" href="/C.html" title=c>class="token keyword">namespaclass="tags" href="/C.html" title=c>ce stdclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{string s class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token string">"hello world hello everyone"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;size_t n class="tags" href="/C.html" title=c>class="token operator">= sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">findclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>char">'o'class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;   class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 查找字符 'o'࿰class="tags" href="/C.html" title=c>c;从字符串开始class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< n class="tags" href="/C.html" title=c>class="token operator"><< endlclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;       class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 输出:4n class="tags" href="/C.html" title=c>class="token operator">= sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">findclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>char">'o'class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, n class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;   class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 从上次位置之后再查找class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< n class="tags" href="/C.html" title=c>class="token operator"><< endlclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;       class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 输出:7class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>

解释:

  • 与字符串查找相比࿰class="tags" href="/C.html" title=c>c;单字符查找更简单࿰class="tags" href="/C.html" title=c>c;参数是一个字符。
  • <class="tags" href="/C.html" title=c>code>find()class="tags" href="/C.html" title=c>code> 会返回字符在字符串中第一次出现的位置࿰class="tags" href="/C.html" title=c>c;如果找到了多次࿰class="tags" href="/C.html" title=c>c;可以通过连续查找进一步获取后续结果。

3. 查找失败处理

通过判断返回值是否为 <class="tags" href="/C.html" title=c>code>nposclass="tags" href="/C.html" title=c>code>࿰class="tags" href="/C.html" title=c>c;可以确实子串是否存在:

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><iostream>
class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><string>
class="tags" href="/C.html" title=c>class="token keyword">using class="tags" href="/C.html" title=c>class="token keyword">namespaclass="tags" href="/C.html" title=c>ce stdclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{string s class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token string">"hello world hello everyone"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;string str class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token string">"bit"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;size_t n class="tags" href="/C.html" title=c>class="token operator">= sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">findclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(strclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">if class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(n class="tags" href="/C.html" title=c>class="token operator">!= stringclass="tags" href="/C.html" title=c>class="token double-class="tags" href="/C.html" title=c>colon punclass="tags" href="/C.html" title=c>ctuation">::nposclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token string">"找到࿰class="tags" href="/C.html" title=c>c;位置是:" class="tags" href="/C.html" title=c>class="token operator"><< n class="tags" href="/C.html" title=c>class="token operator"><< endlclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">} class="tags" href="/C.html" title=c>class="token keyword">else class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token string">"没有找到" class="tags" href="/C.html" title=c>class="token operator"><< endlclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>

解释:

  • <class="tags" href="/C.html" title=c>code>nposclass="tags" href="/C.html" title=c>code> 是一个特殊值࿰class="tags" href="/C.html" title=c>c;代表未找到子串。
  • 可以通过判断 <class="tags" href="/C.html" title=c>code>n == string::nposclass="tags" href="/C.html" title=c>code> 确定查找是否成功࿰class="tags" href="/C.html" title=c>c;并做对应处理。

💯三、应用提示

1. 迭代查找子串

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

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><iostream>
class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><string>
class="tags" href="/C.html" title=c>class="token keyword">using class="tags" href="/C.html" title=c>class="token keyword">namespaclass="tags" href="/C.html" title=c>ce stdclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{string s class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token string">"hello world hello everyone"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;string str class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token string">"llo"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;size_t pos class="tags" href="/C.html" title=c>class="token operator">= sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">findclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(strclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">while class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(pos class="tags" href="/C.html" title=c>class="token operator">!= stringclass="tags" href="/C.html" title=c>class="token double-class="tags" href="/C.html" title=c>colon punclass="tags" href="/C.html" title=c>ctuation">::nposclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token string">"找到:" class="tags" href="/C.html" title=c>class="token operator"><< pos class="tags" href="/C.html" title=c>class="token operator"><< endlclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;pos class="tags" href="/C.html" title=c>class="token operator">= sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">findclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(strclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, pos class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 继续查找class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>

输出:

<class="tags" href="/C.html" title=c>code>找到:2
找到:14
class="tags" href="/C.html" title=c>code>

2. 提取上下文信息

<class="tags" href="/C.html" title=c>code>find()class="tags" href="/C.html" title=c>code> 函数还可以结合 <class="tags" href="/C.html" title=c>code>substr()class="tags" href="/C.html" title=c>code> 提取匹配子串周围的上下文:

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><iostream>
class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><string>
class="tags" href="/C.html" title=c>class="token keyword">using class="tags" href="/C.html" title=c>class="token keyword">namespaclass="tags" href="/C.html" title=c>ce stdclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{string s class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token string">"hello world hello everyone"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;string str class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token string">"world"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;size_t pos class="tags" href="/C.html" title=c>class="token operator">= sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">findclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(strclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">if class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(pos class="tags" href="/C.html" title=c>class="token operator">!= stringclass="tags" href="/C.html" title=c>class="token double-class="tags" href="/C.html" title=c>colon punclass="tags" href="/C.html" title=c>ctuation">::nposclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{size_t start class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(pos class="tags" href="/C.html" title=c>class="token operator">> class="tags" href="/C.html" title=c>class="token number">5class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token operator">? pos class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">5 class="tags" href="/C.html" title=c>class="token operator">: class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;size_t len class="tags" href="/C.html" title=c>class="token operator">= strclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">lengthclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token number">10class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token string">"上下文:" class="tags" href="/C.html" title=c>class="token operator"><< sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">substrclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(startclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, lenclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token operator"><< endlclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>

输出:

<class="tags" href="/C.html" title=c>code>上下文:hello world hello
class="tags" href="/C.html" title=c>code>

3. 忽略大小写查找

<class="tags" href="/C.html" title=c>code>find()class="tags" href="/C.html" title=c>code> 函数默认区分大小写。如果需要忽略大小写࿰class="tags" href="/C.html" title=c>c;可以先将字符串转换为小写后再查找:

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><iostream>
class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><string>
class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><algorithm>
class="tags" href="/C.html" title=c>class="token keyword">using class="tags" href="/C.html" title=c>class="token keyword">namespaclass="tags" href="/C.html" title=c>ce stdclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{string s class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token string">"Hello World Hello Everyone"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;string str class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token string">"hello"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 转换为小写class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">transformclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">beginclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">endclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">beginclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token double-class="tags" href="/C.html" title=c>colon punclass="tags" href="/C.html" title=c>ctuation">::tolowerclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">transformclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(strclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">beginclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, strclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">endclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, strclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">beginclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token double-class="tags" href="/C.html" title=c>colon punclass="tags" href="/C.html" title=c>ctuation">::tolowerclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;size_t pos class="tags" href="/C.html" title=c>class="token operator">= sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">findclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(strclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">if class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(pos class="tags" href="/C.html" title=c>class="token operator">!= stringclass="tags" href="/C.html" title=c>class="token double-class="tags" href="/C.html" title=c>colon punclass="tags" href="/C.html" title=c>ctuation">::nposclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token string">"找到࿰class="tags" href="/C.html" title=c>c;位置是:" class="tags" href="/C.html" title=c>class="token operator"><< pos class="tags" href="/C.html" title=c>class="token operator"><< endlclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">} class="tags" href="/C.html" title=c>class="token keyword">else class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token string">"没有找到" class="tags" href="/C.html" title=c>class="token operator"><< endlclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>

输出:

<class="tags" href="/C.html" title=c>code>找到࿰class="tags" href="/C.html" title=c>c;位置是:0
class="tags" href="/C.html" title=c>code>

💯四、小结

C++ 中的 <class="tags" href="/C.html" title=c>code>find()class="tags" href="/C.html" title=c>code> 函数是字符串操作中极其重要的工具。通过对其多种使用方法的学习࿰class="tags" href="/C.html" title=c>c;我们可以解决多种字符串匹配问题:

  • 基本查找功能࿰class="tags" href="/C.html" title=c>c;用于快速定位子串或字符;
  • 通过 <class="tags" href="/C.html" title=c>code>nposclass="tags" href="/C.html" title=c>code> 判断查找失败࿰class="tags" href="/C.html" title=c>c;确保程序稳定性;
  • 结合循环࿰class="tags" href="/C.html" title=c>c;实现多次查找或全文匹配;
  • 配合 <class="tags" href="/C.html" title=c>code>substr()class="tags" href="/C.html" title=c>code> 等函数࿰class="tags" href="/C.html" title=c>c;提取匹配上下文。

此外࿰class="tags" href="/C.html" title=c>c;在实际开发中࿰class="tags" href="/C.html" title=c>c;还可以通过忽略大小写等变形用法来满足不同场景需求。希望本文能够帮助读者深入理解并熟练掌握 <class="tags" href="/C.html" title=c>code>find()class="tags" href="/C.html" title=c>code> 函数的使用。


class="tags" href="/C.html" title=c>c="https://img-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/00d4fdeb7b0d4dbb99e88f325ef249d8.gif#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" width="1000" height="100" />


class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />

学习C++的建议

C++ 是一门强大且广泛应用的编程语言࿰class="tags" href="/C.html" title=c>c;无论是系统开发、游戏开发还是高性能计算࿰class="tags" href="/C.html" title=c>c;它都有不可替代的地位。然而࿰class="tags" href="/C.html" title=c>c;学习C++的道路可能是陡峭的࿰class="tags" href="/C.html" title=c>c;为了帮助你更高效地掌握这门语言࿰class="tags" href="/C.html" title=c>c;我总结了一些学习建议和经验。


1. 理解C++的基础
学习重点:

  1. 掌握基本语法
    • 学会声明变量、条件语句、循环、函数等核心语法。
    • 掌握数组、指针、引用等基础知识。
  2. 熟悉标准输入输出
    • 掌握 <class="tags" href="/C.html" title=c>code>class="tags" href="/C.html" title=c>cinclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>class="tags" href="/C.html" title=c>coutclass="tags" href="/C.html" title=c>code> 的用法。
    • 理解格式化输出࿰class="tags" href="/C.html" title=c>c;例如 <class="tags" href="/C.html" title=c>code>std::setwclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>std::fixedclass="tags" href="/C.html" title=c>code>。
  3. 记住编译器的角色
    • 理解源代码如何通过编译器转换为可执行程序࿰class="tags" href="/C.html" title=c>c;选择一款好的IDE(如Visual Studio、Clion、VSCode)。
class="tags" href="/C.html" title=c>ckquote>

建议:用大量小程序练习这些基础语法࿰class="tags" href="/C.html" title=c>c;比如写一个计算器、模拟猜数字游戏等。

class="tags" href="/C.html" title=c>ckquote>

2. 深入学习C++的特性
学习重点:

  1. 面向对象编程(OOP)
    • 理解类和对象࿰class="tags" href="/C.html" title=c>c;熟悉如何定义类、成员变量和成员函数。
    • 掌握封装、继承、多态三大核心特性。
  2. 内存管理
    • 理解指针的用法࿰class="tags" href="/C.html" title=c>c;掌握动态内存分配(<class="tags" href="/C.html" title=c>code>newclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>deleteclass="tags" href="/C.html" title=c>code>)。
    • 学习如何避免内存泄漏࿰class="tags" href="/C.html" title=c>c;熟悉智能指针(如<class="tags" href="/C.html" title=c>code>std::unique_ptrclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>std::shared_ptrclass="tags" href="/C.html" title=c>code>)。
  3. 标准模板库(STL)
    • 熟悉常用的容器(如<class="tags" href="/C.html" title=c>code>veclass="tags" href="/C.html" title=c>ctorclass="tags" href="/C.html" title=c>code>、<class="tags" href="/C.html" title=c>code>mapclass="tags" href="/C.html" title=c>code>、<class="tags" href="/C.html" title=c>code>setclass="tags" href="/C.html" title=c>code>)和算法(如<class="tags" href="/C.html" title=c>code>sortclass="tags" href="/C.html" title=c>code>、<class="tags" href="/C.html" title=c>code>findclass="tags" href="/C.html" title=c>code>)。
    • 掌握迭代器的用法。
  4. 异常处理
    • 学习使用<class="tags" href="/C.html" title=c>code>try-class="tags" href="/C.html" title=c>catclass="tags" href="/C.html" title=c>chclass="tags" href="/C.html" title=c>code>块处理异常。
    • 理解异常的用途以及如何设计健壮的代码。
class="tags" href="/C.html" title=c>ckquote>

建议:尝试开发一个小型项目࿰class="tags" href="/C.html" title=c>c;比如一个学生管理系统࿰class="tags" href="/C.html" title=c>c;综合应用类、STL、指针和动态内存分配。

class="tags" href="/C.html" title=c>ckquote>

3. 掌握进阶内容
学习重点:

  1. 模板
    • 理解函数模板和类模板࿰class="tags" href="/C.html" title=c>c;掌握泛型编程思想。
    • 学习模板特化和模板元编程的基本概念。
  2. 多线程与并发
    • 学习 C++11 提供的多线程支持(如<class="tags" href="/C.html" title=c>code>std::threadclass="tags" href="/C.html" title=c>code>)。
    • 熟悉互斥锁(<class="tags" href="/C.html" title=c>code>std::mutexclass="tags" href="/C.html" title=c>code>)和条件变量(<class="tags" href="/C.html" title=c>code>std::class="tags" href="/C.html" title=c>condition_variableclass="tags" href="/C.html" title=c>code>)。
  3. C++与C的兼容性
    • 学习如何在C++中使用C语言代码࿰class="tags" href="/C.html" title=c>c;理解C和C++的区别。
    • 掌握C风格字符串(<class="tags" href="/C.html" title=c>code>class="tags" href="/C.html" title=c>char[]class="tags" href="/C.html" title=c>code>)和C++字符串(<class="tags" href="/C.html" title=c>code>std::stringclass="tags" href="/C.html" title=c>code>)的转换。
class="tags" href="/C.html" title=c>ckquote>

建议:在这一阶段࿰class="tags" href="/C.html" title=c>c;可以挑战更复杂的项目࿰class="tags" href="/C.html" title=c>c;比如开发一个小型的HTTP服务器࿰class="tags" href="/C.html" title=c>c;学习网络编程并结合多线程。

class="tags" href="/C.html" title=c>ckquote>

4. 学习资源与实践方法
学习资源:

  1. 书籍
    • 《C++ Primer》:非常适合初学者的经典书籍。
    • 《Effeclass="tags" href="/C.html" title=c>ctive C++》:进阶学习C++最佳实践的指南。
    • 《The C++ Programming Language》:Bjarne Stroustrup(C++之父)的权威著作。
  2. 在线课程
    • Coursera 上的 C++ 编程课程。
    • YouTube 上免费的 C++ 系列教程。
  3. 社区与文档
    • 参与C++相关的论坛(如CSDN、Staclass="tags" href="/C.html" title=c>ck Overflow)。
    • 阅读官方文档(https://en.class="tags" href="/C.html" title=c>cppreferenclass="tags" href="/C.html" title=c>ce.class="tags" href="/C.html" title=c>com)。

实践方法:

  1. 多写代码࿰class="tags" href="/C.html" title=c>c;多调试
    • 每学一个概念后࿰class="tags" href="/C.html" title=c>c;写至少两个示例代码并进行调试。
  2. 做小项目
    • 从简单的控制台程序开始࿰class="tags" href="/C.html" title=c>c;比如文件读写、计算器、小游戏等。
    • 慢慢过渡到图形界面或网络程序开发。
  3. 阅读他人代码
    • 阅读开源项目的代码࿰class="tags" href="/C.html" title=c>c;理解优秀代码的设计思路。
  4. 参加编程比赛
    • 比如 LeetCode 或 Codeforclass="tags" href="/C.html" title=c>ces࿰class="tags" href="/C.html" title=c>c;可以帮助你提升算法能力和C++的熟练度。

5. 保持耐心与兴趣
学习C++可能会面临以下困难:

  1. 复杂的语法:如模板、智能指针、多线程等。
  2. 调试困难:指针错误、内存泄漏、未定义行为可能让人头疼。

如何应对:

  • 将大问题拆解成小问题࿰class="tags" href="/C.html" title=c>c;逐步解决。
  • 不断重复基础知识࿰class="tags" href="/C.html" title=c>c;以加深理解。
  • 保持兴趣࿰class="tags" href="/C.html" title=c>c;尝试一些有趣的项目࿰class="tags" href="/C.html" title=c>c;比如写一个游戏、制作一个简单的图形程序。

小结
学习C++需要一个循序渐进的过程࿰class="tags" href="/C.html" title=c>c;从基础语法到面向对象编程࿰class="tags" href="/C.html" title=c>c;再到进阶的模板和并发编程࿰class="tags" href="/C.html" title=c>c;每一步都需要耐心和实践。通过充分利用学习资源、进行大量编码练习࿰class="tags" href="/C.html" title=c>c;以及挑战实际项目࿰class="tags" href="/C.html" title=c>c;你一定可以成为C++的高手。记住࿰class="tags" href="/C.html" title=c>c;学习的核心在于理解࿰class="tags" href="/C.html" title=c>c;而非死记硬背。

祝你学有所成࿰class="tags" href="/C.html" title=c>c;享受C++编程的乐趣!


class="tags" href="/C.html" title=c>class="blog-vote-box">

http://www.ppmy.cn/devtools/150075.html

相关文章

音频DSP的发展历史

音频数字信号处理&#xff08;DSP&#xff09;的发展历史是电子技术、计算机科学和音频工程共同进步的结果。这个领域的进展不仅改变了音乐制作、音频后期制作和通信的方式&#xff0c;也影响了音频设备的设计和功能。以下是对音频DSP发展历史的概述&#xff1a; 早期概念和理论…

【MySQL数据库】基础总结

目录 前言 一、概述 二、 SQL 1. SQL通用语法 2. SQL分类 3. DDL 3.1 数据库操作 3.2 表操作 4. DML 5. DQL 5.1 基础查询 5.2 条件查询 5.3 聚合函数 5.4 分组查询 5.5 排序查询 5.6 分页查询 6. DCL 6.1 管理用户 6.2 权限控制 三、数据类型 1. 数值类…

2021 年 3 月青少年软编等考 C 语言五级真题解析

目录 T1. 红与黑思路分析T2. 密室逃脱思路分析T3. 求逆序对数思路分析T4. 最小新整数思路分析T1. 红与黑 有一间长方形的房子,地上铺了红色、黑色两种颜色的正方形瓷砖。你站在其中一块黑色的瓷砖上,只能向相邻的黑色瓷砖移动。请写一个程序,计算你总共能够到达多少块黑色的…

Day31补代码随想录20250110贪心算法5 56.合并区间|738.单调递增的数字|968.监控二叉树(可跳过)

【先跳过监控二叉树】 56.合并区间 题目 以数组 intervals 表示若干个区间的集合&#xff0c;其中单个区间为 intervals[i] [start<sub>i</sub>, end<sub>i</sub>] 。请你合并所有重叠的区间&#xff0c;并返回 一个不重叠的区间数组&#xff0c;该…

[程序设计]—代理模式

[程序设计]—代理模式&#x1f473; 本文章记录学习于——52.面向切面&#xff1a;AOP-场景模拟_哔哩哔哩_bilibili 最近闲来无事&#xff0c;在学习Spring的源码&#xff1a; 后面慢慢更新源码系列blog&#xff0c;希望多多关注&#x1f64f;&#x1f64f; 目前已经总结的b…

MySQL程序之:简要概述

MySQL安装中有许多不同的程序。本节简要概述了它们。后面的部分提供了每个程序的更详细描述&#xff0c;但NDB集群程序除外。每个程序的描述表明了它的调用语法和它支持的选项。&#xff0c;“NDB集群程序”&#xff0c;描述了特定于NDB集群的程序。 大多数MySQL发行版包括所有…

webrtc之rtc::ArrayView<const uint8_t>

rtc::ArrayView<const uint8_t> 是 WebRTC&#xff08;或其他基于 rtc 命名空间的库&#xff09;中常见的一个类型&#xff0c;它通常用于表示一块 只读的内存区域&#xff0c;该内存区域由一系列 uint8_t 类型&#xff08;无符号 8 位整数&#xff09;元素组成。 1. rt…

vue3+elementPlus之后台管理系统(从0到1)(day1)

vue3官方文档&#xff1a;https://cn.vuejs.org/guide/introduction.html 1、项目创建 确保电脑已安装node 查看命令&#xff1a; node -v进入项目目录&#xff0c;创建项目 npm init vuelatest Need to install the following packages: create-vue3.13.0 Ok to procee…