C++ std::string::find_first_of()函数(在字符串中搜索与其参数中指定的任何字符匹配的第一个字符)
Posted Dontla
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 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 <string>
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 <iostream> // std::cout
#include <string> // std::string
#include <cstddef> // 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 <iostream> // std::cout
#include <string> // std::string
#include <cstddef> // 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()函数(在字符串中查找内容)
以上是关于C++ std::string::find_first_of()函数(在字符串中搜索与其参数中指定的任何字符匹配的第一个字符)的主要内容,如果未能解决你的问题,请参考以下文章
[C++]C++入门到入土篇 HelloWorld 解析 && C++入门