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>::find_first_of
std::basic_string<CharT,Traits,Allocator>::find_first_of
| (1) | ||
| size_type find_first_of( const basic_string& str, size_type pos = 0 ) const; |
(C++11 å) | |
| constexpr size_type find_first_of( const basic_string& str, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â size_type pos = 0 ) const noexcept; |
(C++11 èµ·) (C++20 å) |
|
| (2) | ||
| size_type find_first_of( const CharT* s, size_type pos, size_type count ) const; |
(C++11 å) | |
| constexpr size_type find_first_of( const CharT* s, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â size_type pos, size_type count ) const; |
(C++11 èµ·) | |
| (3) | ||
| size_type find_first_of( const CharT* s, size_type pos = 0 ) const; |
(C++20 å) | |
| constexpr size_type find_first_of( const CharT* s, size_type pos = 0 ) const; |
(C++20 èµ·) | |
| (4) | ||
| size_type find_first_of( CharT ch, size_type pos = 0 ) const; |
(C++11 å) | |
| size_type find_first_of( CharT ch, size_type pos = 0 ) const noexcept; |
(C++11 èµ·) (C++20 å) |
|
| constexpr size_type find_first_of( CharT ch, size_type pos = 0 ) const noexcept; |
(C++20 èµ·) | |
| (5) | ||
| template < class T > size_type |
(C++17 èµ·) (C++20 å) |
|
| template < class T > constexpr size_type |
(C++20 èµ·) | |
寻æ¾çäºç»å®å符åºåä¸å符ä¹ä¸çé¦ä¸ªå符ãæç´¢åªèèåºé´ [pos, size()) ãè¥åºé´ä¸ä¸åå¨å符ï¼åè¿å npos ã
1) 寻æ¾çäº
str ä¸å符ä¹ä¸çé¦ä¸ªå符ã2) 寻æ¾çäºèå´
[s, s+count) ä¸å符ä¸å符ä¹ä¸çé¦ä¸ªåç¬¦ãæ¤èå´è½å
å«ç©ºå符ã3) 寻æ¾çäº
s ææåçå符串ä¸å符ä¹ä¸çé¦ä¸ªå符ãç±é¦ä¸ªç©ºå符ï¼ç¨ Traits::length(s) ç¡®å®å符串é¿åº¦ã4) 寻æ¾çäº
ch çé¦ä¸ªå符ã5) å¦åç¨ std::basic_string_view<CharT, Traits> sv = t; éå¼è½¬æ¢
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 æåä¸éè½½å³è®®ãåæ°
| str | - | é´å«è¦æç´¢çå符ç string |
| pos | - | æç´¢å¼å§çä½ç½® |
| count | - | é´å«è¦æç´¢çå符çå符串é¿åº¦ |
| s | - | æåé´å«è¦æç´¢çå符çå符串çæé |
| ch | - | é´å«è¦æç´¢çå符çå符 |
| t | - | é´å«è¦æç´¢çå符ç对象ï¼å¯è½¬æ¢ä¸º std::basic_string_view ï¼ |
è¿åå¼
æ¾å°çå符çä½ç½®ï¼æè¥æ¾ä¸å°è¿ç§å符å为 npos ã
å¼å¸¸
5)
noexcept è§å®ï¼Â Â
noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)
注æ
ç¨ Traits::eq() è¿è¡æ¯è¾ã
ç¼ºé·æ¥å
ä¸åæ´æ¹è¡ä¸ºçç¼ºé·æ¥å追溯å°åºç¨äºä»¥ååºçç C++ æ åã
| DR | åºç¨äº | åºçæ¶çè¡ä¸º | æ£ç¡®è¡ä¸º |
|---|---|---|---|
| LWG 2946 | C++17 | string_view éè½½å¨æäºæ
åµä¸å¯¼è´æ§ä¹
|
éè¿ä½¿ä¹ä¸ºæ¨¡æ¿æ¥é¿å |
示ä¾
è¿è¡æ¤ä»£ç
#include <iostream> #include <string>  int main() { // æµè¯å符串 std::string str = std::string("Hello World!");  // æç´¢ç¨çå符串åå符 std::string search_str = std::string("o"); const char* search_cstr = "Good Bye!";  std::cout << str.find_first_of(search_str) << '\n'; std::cout << str.find_first_of(search_str, 5) << '\n'; std::cout << str.find_first_of(search_cstr) << '\n'; std::cout << str.find_first_of(search_cstr, 0, 4) << '\n'; // 'x' ä¸å¨ "Hello World' ä¸ï¼ä»èå®å°è¿å std::string::npos std::cout << str.find_first_of('x') << '\n'; }
å¯è½çè¾åºï¼
4 7 1 4 18446744073709551615
åé
| äºå符串ä¸å¯»æ¾å符 (å ¬å¼æå彿°) | |
| 寻æ¾å串çæå䏿¬¡åºç° (å ¬å¼æå彿°) | |
| 寻æ¾å符ç馿¬¡ç¼ºå¤± (å ¬å¼æå彿°) | |
| 寻æ¾å符çæå䏿¬¡åºç° (å ¬å¼æå彿°) | |
| 寻æ¾å符çæå䏿¬¡ç¼ºå¤± (å ¬å¼æå彿°) | |
| è¿åä»
ç±å¦ä¸åèåç¬¦ä¸²ä¸æ¾å°çåç¬¦ç»æçæå¤§èµ·å§æ®µçé¿åº¦ (彿°) |