栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ std::string::find

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ std::string::find

文章目录
    • cppman std::string::find_first_of
    • 上面文档中的代码示例

cppman std::string::find_first_of
std::string::find_first_of(3)                                         C++ Programmer's Manual                                         std::string::find_first_of(3)



NAME
       std::string::find_first_of - Find character in string	//在字符串中查找字符

TYPE
       public member function

SYNOPSIS
       #include 


       C++98

       +--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
       | string (1)   | size_t find_first_of (const string& str, size_t pos = 0) const;                                                                            |
       +--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
       |c-string (2)  | size_t find_first_of (const char* s, size_t pos = 0) const;                                                                                |
       +--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
       | buffer (3)   | size_t find_first_of (const char* s, size_t pos, size_t n) const;                                                                          |
       +--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
       |character (4) | size_t find_first_of (char c, size_t pos = 0) const;                                                                                       |
       +--------------+--------------------------------------------------------------------------------------------------------------------------------------------+


       +--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
       | string (1)   | size_t find_first_of (const string& str, size_t pos = 0) const noexcept;                                                                   |
       +--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
       |c-string (2)  | size_t find_first_of (const char* s, size_t pos = 0) const;                                                                                |
       +--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
       | buffer (3)   | size_t find_first_of (const char* s, size_t pos, size_t n) const;                                                                          |
       +--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
       |character (4) | size_t find_first_of (char c, size_t pos = 0) const noexcept;                                                                              |
       +--------------+--------------------------------------------------------------------------------------------------------------------------------------------+


DESCRIPTION
       Searches the string for the first character that matches any of the characters specified in its arguments.
       When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences before pos.
       Notice  that  it  is  enough  for  one  single  character  of  the sequence to match (not all of them).  See string::find for a function that matches entire
       sequences.
       //在字符串中搜索与其参数中指定的任何字符匹配的第一个字符。
        //当指定 pos 时,搜索仅包括位置 pos 处或之后的字符,忽略 pos 之前任何可能出现的字符。
        //请注意,匹配序列中的一个字符(不是所有字符)就足够了。 有关匹配整个序列的函数,请参见 string::find。

PARAMETERS
       str    Another string with the characters to search for.
       //另一个包含要搜索的字符的字符串。

       pos    Position of the first character in the string to be considered in the search.
              If this is greater than the string length, the function never finds matches.
              Note: The first character is denoted by a value of 0 (not 1): A value of 0 means that the entire string is searched.
              //要在搜索中考虑的字符串中第一个字符的位置。
               //如果这大于字符串长度,则函数永远不会找到匹配项。
               //注意:第一个字符由值 0(不是 1)表示:值 0 表示搜索整个字符串。

       s      Pointer to an array of characters.
              If argument n is specified (3), the first n characters in the array are searched for.
              Otherwise (2), a null-terminated sequence is expected: the length of the sequence with the characters to match is determined by the first  occurrence
              of a null character.
              //指向字符数组的指针。
               //如果指定了参数 n (3),则搜索数组中的前 n 个字符。
               //否则(2),一个空终止的序列是预期的:与要匹配的字符的序列的长度由空字符的第一次出现决定。

       n      Number of character values to search for.
       //要搜索的字符值的数量。

       c      Individual character to be searched for.
              size_t is an unsigned integral type (the same as member type string::size_type).
              //要搜索的单个字符。
               //size_t 是无符号整数类型(与成员类型 string::size_type 相同)。

RETURN VALUE
       The position of the first character that matches.
       If no matches are found, the function returns string::npos.
       size_t is an unsigned integral type (the same as member type string::size_type).
       //匹配的第一个字符的位置。
        //如果未找到匹配项,则该函数返回 string::npos。
        //size_t 是无符号整数类型(与成员类型 string::size_type 相同)。

EXAMPLE
         // string::find_first_of
         #include        // std::cout
         #include          // std::string
         #include         // std::size_t
         int main ()
         {
           std::string str ("Please, replace the vowels in this sentence by asterisks.");
           std::size_t found = str.find_first_of("aeiou");
           while (found!=std::string::npos)
           {
             str[found]='*';
             found=str.find_first_of("aeiou",found+1);
           }
           std::cout << str << 'n';
           return 0;
         }

         Pl**s*, r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks.


COMPLEXITY
       Unspecified, but generally up to linear in length()-pos times the number of characters to match (worst case).

ITERATOR VALIDITY
       No changes.

DATA RACES
       The object is accessed.

EXCEPTION SAFETY
       If s does not point to an array long enough, it causes undefined behavior.
       Otherwise, the function never throws exceptions (no-throw guarantee).

SEE ALSO
       string::find(3)
              Find content in string  (public member function)

       string::find_last_of(3)
              Find character in string from the end  (public member function)

       string::find_first_not_of(3)
              Find absence of character in string  (public member function)

       string::replace(3)
              Replace portion of string  (public member function)

       string::substr(3)
              Generate substring  (public member function)

REFERENCE
       cplusplus.com, 2000-2015 - All rights reserved.



cplusplus.com                                                                2022-05-13                                               std::string::find_first_of(3)
(END)

上面文档中的代码示例
#pragma warning(disable : 4996)
// string::find_first_of
#include        // std::cout
#include          // std::string
#include         // std::size_t
int main()
{
    std::string str("Please, replace the vowels in this sentence by asterisks.");   //"请用星号替换这句话中的元音"
    std::size_t found = str.find_first_of("aeiou");
    while (found != std::string::npos)
    {
        str[found] = '*';
        found = str.find_first_of("aeiou", found + 1);
    }
    std::cout << str << 'n';
    return 0;
}

运行结果:

Pl**s*, r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks.

相关函数:C++ std::string::find()函数(在字符串中查找内容)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/889976.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号