r_r*_*jan 3 c++ string stl function count
例如.
abcfgf
abdef
Run Code Online (Sandbox Code Playgroud)
ans = 2 {"ab"是一个常见的起始字符}
Ami*_*ory 10
您可以使用std::mismatch
,它返回一对迭代器,指示序列开始不同的各个迭代器.
例如,要计算公共前缀的长度,您可以执行以下操作:
#include <iostream>
#include <iterator>
#include <string>
#include <algorithm>
using namespace std;
int main() {
const string l = "abcde", r = "abcdfgh";
cout << distance(begin(l), mismatch(begin(l), end(l), begin(r)).first) << endl;
}
Run Code Online (Sandbox Code Playgroud)