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++ 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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。