std::basic_string<CharT,Traits,Allocator>::rfind
: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| (1) | ||
size_type rfind( const basic_string& str, size_type pos = npos ) const; |
(C++11) | |
size_type rfind( const basic_string& str, size_type pos = npos ) const noexcept; |
(C++11) (C++20) |
|
constexpr size_type rfind( const basic_string& str, size_type pos = npos ) const noexcept; |
(C++20) | |
| (2) | ||
size_type rfind( const CharT* s, size_type pos, size_type count ) const; |
(C++20) | |
constexpr size_type rfind( const CharT* s, size_type pos, size_type count ) const; |
(C++20) | |
| (3) | ||
size_type rfind( const CharT* s, size_type pos = npos ) const; |
(C++20) | |
constexpr size_type rfind( const CharT* s, size_type pos = npos ) const; |
(C++20) | |
| (4) | ||
size_type rfind( CharT ch, size_type pos = npos ) const; |
(C++11) | |
size_type rfind( CharT ch, size_type pos = npos ) const noexcept; |
(C++11) (C++20) |
|
constexpr size_type rfind( CharT ch, size_type pos = npos ) const noexcept; |
(C++20) | |
| (5) | ||
template < class T > size_type rfind( const T& t, size_type pos = npos ) const noexcept(/* see below */); |
(C++17) (C++20) |
|
template < class T > constexpr size_type rfind( const T& t, size_type pos = npos ) const noexcept(/* see below */); |
(C++20) | |
1)
str 2)
[s, s+count) 3)
s Traits::length(s) 4)
ch 5)
std::basic_string_view<CharT, Traits> sv = t; t sv sv std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> true std::is_convertible_v<const T&, const CharT*> false | str | - | |
| pos | - | |
| count | - | |
| s | - | |
| ch | - | |
| t | - | (std::basic_string_view ) |
(str.size()count Traits::length(s) ) pos () pos > size() (pos == npos ) size()
5)
noexcept :
noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)C++
| DR | |||
|---|---|---|---|
| LWG 2946 | C++17 | string_view overload causes ambiguity in some cases
|
avoided by making it a template |
Run this code
#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) << "\" at " << n << '\n';
}
}
int main()
{
std::string::size_type n;
std::string const s = "This is a string";
//
n = s.rfind("is");
print(n, s);
// 4
n = s.rfind("is", 4);
print(n, s);
//
n = s.rfind('s');
print(n, s);
//
n = s.rfind('q');
print(n, s);
}
:
found: "is a string" at 5
found: "is is a string" at 2
found: "string" at 10
not found
| () | |
| () | |
| () | |
| () | |
| () |