C++
字符串 | Strings

std::basic_string::find_first_of

性病:基本[医]字符串:查找[医]第一[医]成

size_type find_first_of( const basic_string& str, size_type pos = 0 ) const;(1)
size_type find_first_of( const CharT* s, size_type pos, size_type count ) const;(2)
size_type find_first_of( const CharT* s, size_type pos = 0 ) const;(3)
size_type find_first_of( CharT ch, size_type pos = 0 ) const;(4)
size_type find_first_of( std::basic_string_view<CharT, Traits> sv, size_type pos = 0 ) const(5)(since C++17)

查找第一个字符,该字符等于给定字符序列中的一个字符。搜索只考虑间隔。[pos,,,size()29%。如果该字符不在间隔中,npos会被归还。

1%29发现第一个字符等于str好像find_first_of(std::basic_string_view<CharT, Traits>(str), pos)%28自C++17%29

2%29发现第一个字符等于第一个字符中的一个。count所指向的字符串的字符。s...s可以包含空字符。相当于find_first_of(basic_string(s, count), pos)%28直到C++17%29find_first_of(std::basic_string_view<CharT, Traits>(s, count), pos)%28自C++17%29

3%29查找第一个字符等于字符串中的第一个字符。s字符串的长度由第一个空字符决定。相当于find_first_of(basic_string(s), pos)%28直到C++17%29find_first_of(std::basic_string_view<CharT,Traits>(s), pos)%28自C++17%29

4%29发现第一个字符等于ch...

5%29发现第一个字符等于sv

参数

str-string identifying characters to search for
pos-position at which to begin searching
count-length of character string identifying characters to search for
s-pointer to a character string identifying characters to search for
ch-character to search for
sv-std::basic_string_view to search for

返回值

已发现字符的位置或npos如果找不到这样的角色。

例外

1-4) (none)(until C++11)
1,4) noexcept specification: noexcept 2,3) (none)(since C++11)(until C++14)
1) noexcept specification: noexcept 2-4) (none)(since C++14)
5) noexcept specification: noexcept(since C++17)

注记

traits::eq()用于执行比较。

二次

#include <iostream> #include <string> int main() { // the test string std::string str = std::string("Hello World!" // strings and chars to search for 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' is not in "Hello World', thus it will return std::string::npos std::cout << str.find_first_of('x') << '\n'; }

二次

可能的产出:

二次

4 7 1 4 18446744073709551615

二次

另见

findfind characters in the string (public member function)
rfindfind the last occurrence of a substring (public member function)
find_first_not_offind first absence of characters (public member function)
find_last_offind last occurrence of characters (public member function)
find_last_not_offind last absence of characters (public member function)
strspnreturns the length of the maximum initial segment that consists of only the characters found in another byte string (function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/string/basic[医]字符串/查找[医]第一[医]成