c++ string.find()参数说明
int find(string s, int m, int n)
假设母字符串为str.
该函数实现的是在 str 中的第m个位置往后开始查找目标字符串s的前n个字符,返回找到的第一个出现字符串首字母的位置,若没找到,则返回-1.
说明:
- 第一个参数是要查找的字符串s.
- 第二个参数是母字符串中查找的起始位置。
- 第三个参数是目标字符串的前n个字符。或者说第三个参数为指出字符的个数。
示例:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "ABCDAEFG88991456188A";
int pos1 = str.find("A", 0, 1);
int pos2 = str.find("A", 1, 1); // 从str的1位置开始往后找“A”
int pos3 = str.find("A", 0, 2); // 这里由于2超出了“A”的长度,所以返回-1
int pos4 = str.find("88A", 0, 3); // 从str的3位置开始往后找“88A”
int pos5 = str.find("88A", 0, 2); // 从str的0位置开始往后找“88”
int pos6 = str.find("88A", 9, 2); // 从str的9位置开始往后找“88”
int pos7 = str.find("88A", 0, 1); // 从str的0位置开始往后找“8”
cout << pos1 << endl;
cout << pos2 << endl;
cout << pos3 << endl;
cout << pos4 << endl;
cout << pos5 << endl;
cout << pos6 << endl;
cout << pos7 << endl;
system("pause");
return 0;
}
输出为:
0
4
-1
17
8
17
8