std::find_first_of() 作為 STL 函數
find_first_of() 在一個有用的 STL 函數中,它由字符串調用,並從參數中提供的字符列表(字符串 str)中搜索任何字符的第一個位置。
用法:
size_t find_first_of (const string& str, size_t pos = 0) const;
其中,
const string& str
- 需要搜索的任何字符的集合。size_t pos
- 一個可選參數,默認值為 0。Position 定義調用字符串的起始索引,從哪裏開始搜索。
返回類型: size_t
如果在調用字符串中找到提到的集合的任何字符,則返回索引。如果調用字符串中不存在任何字符,則它返回一個字符串::npos。
如果找到任何字符,它會立即返回索引。
範例1:未傳遞位置參數,因此默認為 0
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str = "includehelp";
string re = "aeiou";
//its searching in the string str
//it searches any character from the string re
//now if any character of re is found then it simply
//returns the position of the character found
cout << "the first vowel appearing in the string:" << str << " is " << str[str.find_first_of(re)] << endl;
return 0;
}
輸出:
the first vowel appearing in the string:includehelp is i
In the above program, In the first case, The invoking string is str, "includehelp" The collection of characters which are being searched "aeiou" -> 'a', 'e', 'i', 'o', 'u' The position argument is not passed so it will start searching from position 0 which is 'i', since 'i' falls in the collection of characters thus it returns 0
範例2:位置參數被傳遞
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str = "includehelp";
string re = "aeiou";
//its searching in the string str
//it searches any character from the string re
//now if any character of re is found then it simply
//returns the position of the character found
//now this will start searching in str from position=1
//thus it skips the fisrt vowel 'i'
cout << "the second vowel appearing in the string:" << str << " is " << str[str.find_first_of(re, 1)] << endl;
return 0;
}
輸出:
the second vowel appearing in the string:includehelp is u
在上述情況下,起始位置為 1,因此它僅從索引 1 開始搜索,跳過第一個索引 (0),即 'i'。因此,找到的集合中的第一個字符是 'u'。
相關用法
- C++ std::find_first_of用法及代碼示例
- C++ std::find_end用法及代碼示例
- C++ std::find用法及代碼示例
- C++ std::find()用法及代碼示例
- C++ std::fill()用法及代碼示例
- C++ std::fill_n()用法及代碼示例
- C++ std::forward_list::sort()用法及代碼示例
- C++ std::front_inserter用法及代碼示例
- C++ std::fstream::close()用法及代碼示例
- C++ std::for_each()用法及代碼示例
- C++ std::max()用法及代碼示例
- C++ std::string::push_back()用法及代碼示例
- C++ std::less_equal用法及代碼示例
- C++ std::is_member_object_pointer模板用法及代碼示例
- C++ std::copy_n()用法及代碼示例
- C++ std::string::insert()用法及代碼示例
- C++ std::is_sorted_until用法及代碼示例
- C++ std::iota用法及代碼示例
- C++ std::numeric_limits::digits用法及代碼示例
- C++ std::string::data()用法及代碼示例
注:本文由純淨天空篩選整理自 std::find_first_of() with examples in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。