命名空间
变体
操作

std::basic_string<CharT,Traits,Allocator>::find

来自 cppreference.cn
< cpp‎ | string‎ | basic_string
 
 
 
std::basic_string
 
size_type find( const basic_string& str, size_type pos = 0 ) const;
(1) (C++11 起无异常抛出)
(C++20 起为 constexpr)
size_type find( const CharT* s, size_type pos, size_type count ) const;
(2) (C++20 起为 constexpr)
size_type find( const CharT* s, size_type pos = 0 ) const;
(3) (C++20 起为 constexpr)
size_type find( CharT ch, size_type pos = 0 ) const;
(4) (C++11 起无异常抛出)
(C++20 起为 constexpr)
template< class StringViewLike >

size_type find( const StringViewLike& t,

                size_type pos = 0 ) const noexcept(/* see below */);
(5) (C++17 起)
(C++20 起为 constexpr)

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

1) 查找第一个等于 str 的子字符串。
2) 查找第一个等于范围 [ss + count) 的子字符串。此范围可能包含空字符。
如果 [ss + count) 不是有效范围,则行为未定义。
3) 查找第一个等于由 s 指向的字符串的子字符串。字符串的长度由第一个空字符使用 Traits::length(s) 确定。
如果 [ss + 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 <iomanip>
#include <iostream>
#include <string>
 
void print(int id, std::string::size_type n, std::string const& s)
{
    std::cout << id << ") ";
    if (std::string::npos == n)
        std::cout << "not found! n == npos\n";
    else
        std::cout << "found @ n = " << n << ", substr(" << n << ") = "
                  << std::quoted(s.substr(n)) << '\n';
}
 
int main()
{
    std::string::size_type n;
    std::string const s = "This is a string"; /*
                             ^  ^  ^
                             1  2  3          */
 
    // search from beginning of string
    n = s.find("is");
    print(1, n, s);
 
    // search from position 5
    n = s.find("is", 5);
    print(2, n, s);
 
    // find a single character
    n = s.find('a');
    print(3, n, s);
 
    // find a single character
    n = s.find('q');
    print(4, n, s);
}

输出

1) found @ n = 2, substr(2) = "is is a string"
2) found @ n = 5, substr(5) = "is a string"
3) found @ n = 8, substr(8) = "a string"
4) not found! n == npos

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 847 C++98 没有异常安全保证 添加了强异常安全保证
LWG 2064 C++11 重载 (3,4) 为 noexcept 已移除
LWG 2946 C++17 重载 (5) 在某些情况下导致歧义 通过使其成为模板来避免
P1148R0 C++11
C++17
重载 (4,5) 的 noexcept 被
LWG2064/LWG2946 意外删除
恢复

[编辑] 另请参阅

查找字符子字符串的第一次出现
(函数) [编辑]
在一个宽字符串中查找另一个宽字符串的首次出现
(函数) [编辑]
查找字符的第一次出现
(函数) [编辑]
在宽字符串中查找宽字符的首次出现
(函数) [编辑]
查找子串的最后一次出现
(公共成员函数) [编辑]
查找字符的首次出现
(公共成员函数) [编辑]
查找字符的首次缺席
(公共成员函数) [编辑]
查找字符的最后一次出现
(公共成员函数) [编辑]
查找字符的最后一次缺席
(公共成员函数) [编辑]
在视图中查找字符
(std::basic_string_view<CharT,Traits> 的公共成员函数) [编辑]
搜索一个范围的元素首次出现的位置
(函数模板) [编辑]