C++ std::string::find()函数(在字符串中查找内容)
Posted Dontla
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ std::string::find()函数(在字符串中查找内容)相关的知识,希望对你有一定的参考价值。
文章目录
mancpp std::string::find
std::string::find(3) C++ Programmer's Manual std::string::find(3)
NAME
std::string::find - Find content in string //在字符串中查找内容
TYPE
public member function //公共成员函数
SYNOPSIS
#include <string>
C++98
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
| string (1) | size_t find (const string& str, size_t pos = 0) const; |
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
|c-string (2) | size_t find (const char* s, size_t pos = 0) const; |
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
| buffer (3) | size_t find (const char* s, size_t pos, size_t n) const; |
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
|character (4) | size_t find (char c, size_t pos = 0) const; |
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
| string (1) | size_t find (const string& str, size_t pos = 0) const noexcept; |
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
|c-string (2) | size_t find (const char* s, size_t pos = 0) const; |
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
| buffer (3) | size_t find (const char* s, size_t pos, size_type n) const; |
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
|character (4) | size_t find (char c, size_t pos = 0) const noexcept; |
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------+
DESCRIPTION
Searches the string for the first occurrence of the sequence specified by its arguments.
When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences that include characters before pos.
Notice that unlike member find_first_of, whenever more than one character is being searched for, it is not enough that just one of these characters match,
but the entire sequence must match.
//在字符串中搜索由其参数指定的序列的第一次出现。
//当指定 pos 时,搜索仅包括位置 pos 处或之后的字符,忽略任何可能出现的包括 pos 之前的字符。
//请注意,与成员 find_first_of 不同,每当搜索多个字符时,仅匹配其中一个字符是不够的,而必须匹配整个序列。
PARAMETERS
str Another string with the subject 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 sequence to match are the first n characters in the array.
Otherwise (2), a null-terminated sequence is expected: the length of the sequence to match is determined by the first occurrence of a null character.
//指向字符数组的指针。
//如果指定了参数 n (3),则要匹配的序列是数组中的前 n 个字符。
//否则(2),一个空终止的序列是预期的:要匹配的序列的长度由空字符的第一次出现决定。
n Length of sequence of characters to match.
//要匹配的字符序列的长度。
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 of the first match.
If no matches were 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
#include <iostream> // std::cout
#include <string> // std::string
int main ()
std::string str ("There are two needles in this haystack with needles.");
std::string str2 ("needle");
// different member versions of find in the same order as above:
std::size_t found = str.find(str2);
if (found!=std::string::npos)
std::cout << "first 'needle' found at: " << found << '\\n';
found=str.find("needles are small",found+1,6);
if (found!=std::string::npos)
std::cout << "second 'needle' found at: " << found << '\\n';
found=str.find("haystack");
if (found!=std::string::npos)
std::cout << "'haystack' also found at: " << found << '\\n';
found=str.find('.');
if (found!=std::string::npos)
std::cout << "Period found at: " << found << '\\n';
// let's replace the first needle:
str.replace(str.find(str2),str2.length(),"preposition");
std::cout << str << '\\n';
return 0;
Notice how parameter pos is used to search for a second instance of the same search string. Output:
//请注意参数 pos 如何用于搜索同一搜索字符串的第二个实例。
//输出:
first 'needle' found at: 14
second 'needle' found at: 44
Period found at: 51
There are two prepositions in this haystack with needles.
COMPLEXITY //复杂度
Unspecified, but generally up to linear in length()-pos times the length of the sequence to match (worst case).
//未指定,但通常在 length()-pos 乘以要匹配的序列长度乘以线性(最坏情况)。
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).
//如果 s 指向数组的长度不够长,则会导致未定义的行为。
//否则,该函数永远不会抛出异常(不抛出保证)。
SEE ALSO
string::rfind(3)
Find last occurrence of content in string (public member function)
//查找字符串中最后一次出现的内容(公共成员函数)
string::find_first_of(3)
Find character 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::find_last_not_of(3)
Find non-matching character in string from the end (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(3)
(END)
上面文档中的代码示例
(VS上跑过)
#pragma warning(disable : 4996)
// string::find
#include <iostream> // std::cout
#include <string> // std::string
int main()
std::string str("There are two needles in this haystack with needles.");
std::string str2("needle");
// different member versions of find in the same order as above:
//不同成员版本的 find 按与上面相同的顺序:
std::size_t found = str.find(str2);
if (found != std::string::npos)
std::cout << "first 'needle' found at: " << found << '\\n'; //first 'needle' found at: 14
found = str.find("needles are small", found + 1, 6); //第二个参数是pos,str开始搜索的位置;第三个参数是n,指要搜索的内容是"needles are small"的前n个字符
if (found != std::string::npos)
std::cout << "second 'needle' found at: " << found << '\\n'; //second 'needle' found at: 44
found = str.find("haystack");
if (found != std::string::npos)
std::cout << "'haystack' also found at: " << found << '\\n'; //'haystack' also found at: 30
found = str.find('.');
if (found != std::string::npos)
std::cout << "Period found at: " <<以上是关于C++ std::string::find()函数(在字符串中查找内容)的主要内容,如果未能解决你的问题,请参考以下文章
C++ std::string::find_last_of()函数(在字符串中搜索与参数中指定的任何字符匹配的最后一个字符)(从后往前找)(文件路径中找文件名,/\兼容windows和linux)
C++ std::string::find_last_of()函数(在字符串中搜索与参数中指定的任何字符匹配的最后一个字符)(从后往前找)(文件路径中找文件名,/\兼容windows和linux)
C++中std::string::find_last_of用法