跳到主要内容

std::string find() 方法

// (1) Const version only
constexpr size_type find( const basic_string& str, size_type pos = 0 ) const noexcept;

// (2) Const version only
constexpr size_type find( const CharT* s, size_type pos, size_type count ) const;

// (3) Const version only
constexpr size_type find( const CharT* s, size_type pos = 0 ) const;

// (4) Const version only
constexpr size_type find( CharT ch, size_type pos = 0 ) const noexcept;

// (5) Const version only
template < class StringViewLike >
constexpr size_type find( const StringViewLike& t, size_type pos = 0 ) const noexcept(/* see below */);

查找第一个与给定字符序列相等的子字符串。
搜索从 pos 开始,即找到的子字符串不得在早于 pos 的位置开始。

  • (1) 查找第一个与 str 相等的子字符串。

  • (2) 查找第一个与范围 [ s, s + count ) 相等的子字符串。
    此范围可能包含空字符。

  • (3) 查找第一个与 s 指向的字符串相等的子字符串。
    字符串的长度由使用 Traits::length(s) 的第一个空字符决定。

  • (4) 查找第一个字符 ch(根据以下正式规则,将其视为单字符子字符串)。

  • (5)t 隐式转换为字符串视图 sv,如同通过 std::basic_string_view<CharT, Traits> sv = t;,然后查找第一个与 sv 相等的子字符串。

    重载决议

    此重载仅在 std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>truestd::is_convertible_v<const StringViewLike&, const CharT*>false 时参与重载决议。

形式上,如果以下所有条件都为 true,则子字符串 str 被认为在位置 xpos 找到

  • xpos >= pos
  • xpos + str.size() <= size()
  • 对于 str 中的所有位置 nTraits::eq(at(xpos + n), str.at(n))

特别是,这意味着

  • 仅当 pos <= size() - str.size() 时才能找到子字符串
  • 当且仅当 pos <= size() 时,在 pos 找到空子字符串
  • 对于非空子字符串,如果 pos >= size(),函数始终返回 npos

参数

  • str - 要搜索的字符串
  • pos - 开始搜索的位置
  • count - 要搜索的子字符串的长度
  • s - 指向要搜索的字符字符串的指针
  • ch - 要搜索的字符
  • t - 要搜索的对象(可转换为 std::basic_string_view

返回值

找到的子字符串的第一个字符的位置,如果未找到此类子字符串,则为 npos

复杂度

重要

本节需要改进。您可以通过编辑此文档页面来帮助我们。

异常

  • (1-4) (无)
  • (5) noexcept 规范
    noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)

示例

#include <string>
#include <iostream>

void print(std::string::size_type n, std::string const &s)
{
if (n == std::string::npos) {
std::cout << "not found\n";
} else {
std::cout << "found: " << s.substr(n) << '\n';
}
}

int main()
{
std::string::size_type n;
std::string const s = "This is a string";

// search from beginning of string
n = s.find("is");
print(n, s);

// search from position 5
n = s.find("is", 5);
print(n, s);

// find a single character
n = s.find('a');
print(n, s);

// find a single character
n = s.find('q');
print(n, s);
}
输出
found: is is a string
found: is a string
found: a string
not found
本文档源自 此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看对本文档进行的所有更改。
悬停查看原始许可证。

std::string find() 方法

// (1) Const version only
constexpr size_type find( const basic_string& str, size_type pos = 0 ) const noexcept;

// (2) Const version only
constexpr size_type find( const CharT* s, size_type pos, size_type count ) const;

// (3) Const version only
constexpr size_type find( const CharT* s, size_type pos = 0 ) const;

// (4) Const version only
constexpr size_type find( CharT ch, size_type pos = 0 ) const noexcept;

// (5) Const version only
template < class StringViewLike >
constexpr size_type find( const StringViewLike& t, size_type pos = 0 ) const noexcept(/* see below */);

查找第一个与给定字符序列相等的子字符串。
搜索从 pos 开始,即找到的子字符串不得在早于 pos 的位置开始。

  • (1) 查找第一个与 str 相等的子字符串。

  • (2) 查找第一个与范围 [ s, s + count ) 相等的子字符串。
    此范围可能包含空字符。

  • (3) 查找第一个与 s 指向的字符串相等的子字符串。
    字符串的长度由使用 Traits::length(s) 的第一个空字符决定。

  • (4) 查找第一个字符 ch(根据以下正式规则,将其视为单字符子字符串)。

  • (5)t 隐式转换为字符串视图 sv,如同通过 std::basic_string_view<CharT, Traits> sv = t;,然后查找第一个与 sv 相等的子字符串。

    重载决议

    此重载仅在 std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>truestd::is_convertible_v<const StringViewLike&, const CharT*>false 时参与重载决议。

形式上,如果以下所有条件都为 true,则子字符串 str 被认为在位置 xpos 找到

  • xpos >= pos
  • xpos + str.size() <= size()
  • 对于 str 中的所有位置 nTraits::eq(at(xpos + n), str.at(n))

特别是,这意味着

  • 仅当 pos <= size() - str.size() 时才能找到子字符串
  • 当且仅当 pos <= size() 时,在 pos 找到空子字符串
  • 对于非空子字符串,如果 pos >= size(),函数始终返回 npos

参数

  • str - 要搜索的字符串
  • pos - 开始搜索的位置
  • count - 要搜索的子字符串的长度
  • s - 指向要搜索的字符字符串的指针
  • ch - 要搜索的字符
  • t - 要搜索的对象(可转换为 std::basic_string_view

返回值

找到的子字符串的第一个字符的位置,如果未找到此类子字符串,则为 npos

复杂度

重要

本节需要改进。您可以通过编辑此文档页面来帮助我们。

异常

  • (1-4) (无)
  • (5) noexcept 规范
    noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)

示例

#include <string>
#include <iostream>

void print(std::string::size_type n, std::string const &s)
{
if (n == std::string::npos) {
std::cout << "not found\n";
} else {
std::cout << "found: " << s.substr(n) << '\n';
}
}

int main()
{
std::string::size_type n;
std::string const s = "This is a string";

// search from beginning of string
n = s.find("is");
print(n, s);

// search from position 5
n = s.find("is", 5);
print(n, s);

// find a single character
n = s.find('a');
print(n, s);

// find a single character
n = s.find('q');
print(n, s);
}
输出
found: is is a string
found: is a string
found: a string
not found
本文档源自 此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看对本文档进行的所有更改。
悬停查看原始许可证。