C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- std::basic_string
- std::basic_string<CharT,Traits,Allocator>::npos
- std::basic_string<CharT,Traits,Allocator>::at
- std::hash (std::string, std::wstring, std::u16string, std::u32string, std::pmr::string, std::pmr::wstring, std::pmr::u16string, std::pmr::u32string)
- std::basic_string<CharT,Traits,Allocator>::assign
- std::basic_string<CharT,Traits,Allocator>::get_allocator
- std::basic_string<CharT,Traits,Allocator>::substr
- std::basic_string<CharT,Traits,Allocator>::basic_string
- std::basic_string<CharT,Traits,Allocator>::operator=
- std::basic_string<CharT,Traits,Allocator>::operator[]
- std::basic_string<CharT,Traits,Allocator>::front
- std::basic_string<CharT,Traits,Allocator>::back
- std::basic_string<CharT,Traits,Allocator>::data
- std::basic_string<CharT,Traits,Allocator>::c_str
- std::basic_string<CharT,Traits,Allocator>::operator basic_string_view
- std::basic_string<CharT,Traits,Allocator>::begin, std::basic_string<CharT,Traits,Allocator>::cbegin
- std::basic_string<CharT,Traits,Allocator>::end, std::basic_string<CharT,Traits,Allocator>::cend
- std::basic_string<CharT,Traits,Allocator>::rbegin, std::basic_string<CharT,Traits,Allocator>::crbegin
- std::basic_string<CharT,Traits,Allocator>::rend, std::basic_string<CharT,Traits,Allocator>::crend
- std::basic_string<CharT,Traits,Allocator>::empty
- std::basic_string<CharT,Traits,Allocator>::size, std::basic_string<CharT,Traits,Allocator>::length
- std::basic_string<CharT,Traits,Allocator>::max_size
- std::basic_string<CharT,Traits,Allocator>::reserve
- std::basic_string<CharT,Traits,Allocator>::capacity
- std::basic_string<CharT,Traits,Allocator>::shrink_to_fit
- std::basic_string<CharT,Traits,Allocator>::clear
- std::basic_string<CharT,Traits,Allocator>::insert
- std::basic_string<CharT,Traits,Allocator>::erase
- std::basic_string<CharT,Traits,Allocator>::push_back
- std::basic_string<CharT,Traits,Allocator>::pop_back
- std::basic_string<CharT,Traits,Allocator>::append
- std::basic_string<CharT,Traits,Allocator>::operator+=
- std::basic_string<CharT,Traits,Allocator>::compare
- std::basic_string<CharT,Traits,Allocator>::starts_with
- std::basic_string<CharT,Traits,Allocator>::ends_with
- std::basic_string<CharT,Traits,Allocator>::replace
- std::basic_string<CharT,Traits,Allocator>::copy
- std::basic_string<CharT,Traits,Allocator>::resize
- std::basic_string<CharT,Traits,Allocator>::swap
- std::basic_string<CharT,Traits,Allocator>::find
- std::basic_string<CharT,Traits,Allocator>::rfind
- std::basic_string<CharT,Traits,Allocator>::find_first_of
- std::basic_string<CharT,Traits,Allocator>::find_first_not_of
- std::basic_string<CharT,Traits,Allocator>::find_last_of
- std::basic_string<CharT,Traits,Allocator>::find_last_not_of
- std::operator+(std::basic_string)
- std::swap(std::basic_string)
- std::literals::string_literals::operator""s
- std::erase, std::erase_if (std::basic_string)
- operator<<,>>(std::basic_string)
- std::getline
- operator==,!=,<,<=,>,>=,<=>(std::basic_string)
- std::stoi, std::stol, std::stoll
- std::stoul, std::stoull
- std::stof, std::stod, std::stold
- std::to_string
- std::to_wstring
- std::basic_string çæ¨å¯¼æå¼
- std::basic_string_view
- ç©ºç»æ¢åèå符串
- ç©ºç»æ¢å¤åèå符串
- ç©ºç»æ¢å®½å符串
- std::char_traits
- 注é
- 容å¨åº
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- æ°å¼åº
- è¾å ¥/è¾åºåº
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >åç¬¦ä¸²åº >std::basic_string > std::basic_string<CharT,Traits,Allocator>::rfind
std::basic_string<CharT,Traits,Allocator>::rfind
| (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 |
(C++17 èµ·) (C++20 å) |
|
| template < class T > constexpr size_type |
(C++20 èµ·) | |
寻æ¾çäºç»å®å符åºåçæåå串ãæç´¢å§äº pos ï¼å³æ¾å°çå串å¿
é¡»ä¸å§äº pos åçä½ç½®ãè¥å° npos æä»»ä½ä¸å°äº size()-1 çå¼ä½ä¸º pos ä¼ éï¼å卿´ä¸ªåç¬¦ä¸²ä¸æç´¢ã
str çæåå串ã[s, s+count) çæååä¸²ãæ¤èå´è½å
å«ç©ºå符ãs ææåçå符串çæåå串ãç±é¦ä¸ªç©ºå符ï¼ç¨ Traits::length(s) ç¡®å®å符串é¿åº¦ãch çæåå符ãt 为 string_view sv ï¼ç¶å寻æ¾çäº sv å
容çæååä¸²ãæ¤éè½½ä»
è¥ std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> 为 true ä¸ std::is_convertible_v<const T&, const CharT*> 为 false æåä¸éè½½å³è®®ãæææ åµä¸åè°ç¨ Traits::eq æ£æ¥ç¸çæ§ã
åæ°
| str | - | è¦æç´¢ç string |
| pos | - | å¼å§æç´¢çä½ç½® |
| count | - | è¦æç´¢çå串é¿åº¦ |
| s | - | æåè¦æç´¢çå符串çæé |
| ch | - | è¦æç´¢çå符 |
| t | - | è¦æç´¢ç对象ï¼å¯è½¬æ¢ä¸º std::basic_string_view ï¼ |
è¿åå¼
æ¾å°çå串çé¦å符ä½ç½®ï¼æè¥æ¾ä¸å°è¿ç§å串å为 npos ãæ³¨æè¿æ¯ä»å符串å¼å§ï¼è鿫尾çåç§»ã
è¥æç´¢ä»»ä½ç©ºåç¬¦ä¸²ï¼ str.size() ã count æ Traits::length(s) 为é¶ï¼ï¼åè¿å pos ï¼ç«å³æ¾å°ç©ºå符串ï¼ï¼é¤é pos > size() ï¼å
æ¬ pos == npos çæ
åµï¼ï¼è¯¥æ
åµä¸è¿å size() ã
å¦åï¼è¥ size() 为é¶ï¼åå§ç»è¿å npos ã
å¼å¸¸
ç¼ºé·æ¥å
ä¸åæ´æ¹è¡ä¸ºçç¼ºé·æ¥å追溯å°åºç¨äºä»¥ååºçç C++ æ åã
| DR | åºç¨äº | åºçæ¶çè¡ä¸º | æ£ç¡®è¡ä¸º |
|---|---|---|---|
| LWG 2946 | C++17 | string_view éè½½å¨æäºæ
åµä¸å¯¼è´æ§ä¹
|
éè¿ä½¿ä¹ä¸ºæ¨¡æ¿æ¥é¿å |
示ä¾
#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
åé
| äºå符串ä¸å¯»æ¾å符 (å ¬å¼æå彿°) | |
| 寻æ¾å符ç馿¬¡åºç° (å ¬å¼æå彿°) | |
| 寻æ¾å符ç馿¬¡ç¼ºå¤± (å ¬å¼æå彿°) | |
| 寻æ¾å符çæå䏿¬¡åºç° (å ¬å¼æå彿°) | |
| 寻æ¾å符çæå䏿¬¡ç¼ºå¤± (å ¬å¼æå彿°) |