c++ string find(), rfind(), find_first_of(),find_last_of()

时间:2020-07-13 15:14:59   收藏:0   阅读:66

find(), rfind()

函数原型:

int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找

int find(const char* s, int pos = 0) const; //查找s第一次出现位置,从pos开始查找

int find(const char* s, int pos, int n) const; //从pos位置查找s的前n个字符第一次位置

int find(const char c, int pos = 0) const; //查找字符c第一次出现位置

int rfind(const string& str, int pos = npos) const; //查找str最后一次位置,从pos开始查找

int rfind(const char* s, int pos = npos) const; //查找s最后一次出现位置,从pos开始查找

int rfind(const char* s, int pos, int n) const; //从pos查找s的前n个字符最后一次位置

int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置

 

find_first_of(),

 

find_last_of()

 

例题: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;
    }
};

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!