std::string::find_last_of是一個字符串類成員函數,用於查找字符串中任何字符最後一次出現的索引。如果字符存在於字符串中,則返回該字符在字符串中最後一次出現的索引,否則返回string::npos。
頭文件:
#include < string >
模板類
template < class T > size_type find_last_of(const T& t, size_type pos = npos ) const noexcept();
語法1:
find_last_of(char ch)
參數:此函數采用給定字符,並返回該字符最後一次出現的位置。
下麵是說明字符串的程序:: find_last_of():
// C++ program to illustrate string::find_last_of
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
// Driver Code
int main()
{
// Given String
string str("Welcome to GeeksforGeeks!");
// Character to be found
char ch = 'e';
// To store the index of last
// character found
size_t found;
// Function to find the las
// character ch in str
found = str.find_last_of(ch);
// If string doesn't have
// character ch present in it
if (found == string::npos) {
cout << "Character " << ch
<< " is not present in"
<< " the given string.";
}
// Else print the last position
// of the character
else {
cout << "Character " << ch
<< " is found at index:"
<< found << endl;
}
}
輸出:
Character e is found at index:21
語法2:
find_last_of(char ch, size_t position)
參數:該函數采用給定的字符和索引,直到執行搜索為止。它返回該字符最後一次出現的位置。
下麵是說明字符串的程序:: find_last_of():
// C++ program to illustrate string::find_last_of
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
// Driver Code
int main()
{
// Given String
string str("Welcome to GeeksforGeeks!");
// Character to be found
char ch = 'e';
// To store the index of last
// character found
size_t found;
// Position till search is performed
int pos = 10;
// Function to find the last
// character ch in str[0, pos]
found = str.find_last_of(ch, pos);
// If string doesn't have
// character ch present in it
if (found == string::npos) {
cout << "Character " << ch
<< " is not present in"
<< " the given string.";
}
// Else print the last position
// of the character
else {
cout << "Character " << ch
<< " is found at index:"
<< found << endl;
}
}
輸出:
Character e is found at index:6
參考文獻: http://www.cplusplus.com/reference/string/string/find_last_of/
相關用法
- C語言 strtok()、strtok_r()用法及代碼示例
- C語言 memset()用法及代碼示例
- C++ std::mismatch()用法及代碼示例
- C++ wcscpy()用法及代碼示例
- C++ wcscmp()用法及代碼示例
- C++ set_symmetric_difference用法及代碼示例
- C++ ratio_equal()用法及代碼示例
- C++ std::equal_to用法及代碼示例
- C++ quick_exit()用法及代碼示例
- C++ multiset lower_bound()用法及代碼示例
- C++ multiset upper_bound()用法及代碼示例
- C++ multiset max_size()用法及代碼示例
- C++ forward_list max_size()用法及代碼示例
- C++ std::allocator()用法及代碼示例
- C++ array data()用法及代碼示例
- C++ multiset size()用法及代碼示例
- C++ ratio_not_equal()用法及代碼示例
注:本文由純淨天空篩選整理自pranjal_g99大神的英文原創作品 std::string::find_last_of in C++ with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。