find(), rfind()
函数原型:
int find(const string& str, int pos = 0) const;
int find(const char* s, int pos = 0) const;
int find(const char* s, int pos, int n) const;
int find(const char c, int pos = 0) const;
int rfind(const string& str, int pos = npos) const;
int rfind(const char* s, int pos = npos) const;
int rfind(const char* s, int pos, int n) const;
int rfind(const char c, int pos = 0) const;
find_first_of(),
用法:str.find_first_of(str1,pos)
说明:从pos位置开始查找str1,从前往后,只要查到str1中的任何一个字符有则返回其在str中的索引值
find_last_of()
用法:str.find_last_of(str1,pos)
说明:从pos位置开始查找,从后往前,查到str1中的任何一个字符则返回其str中的索引值
例题:leetcode 345题:反转字符串中的元音字母
输入:hello
返回:holle
class Solution {
public:
string reverseVowels(string s) {
int left=0,right=s.size()-1;
while(left<right)
{
left=s.find_first_of("aeiouAEIOU",left);
right=s.find_last_of("aeiouAEIOU",right);
if(left<right)
{
swap(s[left++],s[right--]);
}
}
return s;
}
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)