E.
abcfgf
abdef
and=2 {"ab"是一个普通的起始字符}
最佳答案
你可以使用 std::mismatch
,它返回一对迭代器,指示序列开始不同的相应迭代器。
例如,要计算公共(public)前缀的长度,您可以执行以下操作:
#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;
}
关于c++ - C++ std::string 中是否有任何函数可以计算两个字符串的相同起始字符总数或任何最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43953608/