该函数返回目标字符串(key)在源字符串中最后一次匹配的位置
如果没有找到匹配的位置则返回string::npos,是一个无符号整形数字,可以打印看看
string::rfind
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstddef>
using namespace std;
int main(int argc, char* argv[])
{
string str ("i love you do you love me.");
string key ("love");
size_t found = str.rfind(key);
if (found!=string::npos)
str.replace (found,key.length(),"like");
//cout<<string::npos<<endl;
cout << str <<endl;
return 0;
}
最后的运行结果如图所示: