问题遇到的现象和发生背景
在C++ string容器的find函数和rfind函数得到结果是一样的
在练习string容器相关知识时报错。
问题相关代码,请勿粘贴截图
#include<iostream>
#include<string>
using namespace std;
//字符串拼接
void joint()
{
string str1 = "hello";
string str2 = "world";
cout << str1 + str2 << endl;
str1.append(str2);
cout << str1 << endl;
str2.assign("hhhh");
cout << str2 << endl;
}
//字符串查找
void find()
{
string str1 = "hello world";
int pos = str1.find("ld");
cout << pos << endl;
int pos1 = str1.rfind("ld");
cout << pos1 << endl;
}
//字符串比较
//插入和删除
//子串获取
int main()
{
find();
system("pause");
}
运行结果及报错内容
我的解答思路和尝试过的方法
find函数从左到右开始寻找,rfind函数从右到左开始寻找。
我想要达到的结果
pos = 9;
pos1 = 0;