C++ rfind 函数解析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CV2017/article/details/85263406

功能

从后往前查找在当前字符串中的位置,此位置与数组下标类似

用法

int rfind(char c, int pos = npos)  //从 pos 开始从后向前查找字符 c 在当前串中的位置
int rfind(const char *s, int pos = npos)
int rfind(const char *s, int pos, int n = npos)
int rfind(const string &s,int pos = npos)

示例

#include <iostream>

using namespace std;

int main()
{
	string st = "123456789"; // 字符 9 对应的 pos 下标位置是 8,字符 1 对应的 pos 下标位置是 0

	cout << st.rfind("9", 10000000) << endl; // 输入 pos 位置为非常大,从后往前依然能找到字符 9
	cout << st.rfind("9", 12) << endl;
	cout << st.rfind("9", 11) << endl;
	cout << st.rfind("9", 10) << endl;
	cout << st.rfind("9", 9) << endl;
	cout << st.rfind("9", 8) << endl; //输入 pos 位置为 8,从后往前在下标位置 8 刚好找到

	cout << st.rfind("9", 7) << endl; //输入 pos 位置为 7,从后往前就找不到字符 9 了
	cout << st.rfind("9", 6) << endl;

	return 0;
}

输出结果 

猜你喜欢

转载自blog.csdn.net/CV2017/article/details/85263406