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)