我会尝试使用std::mismatch (documentation)
template <class InputIterator1, class InputIterator2>
pair<InputIterator1, InputIterator2>
mismatch (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2 );
Return first position where two ranges differ
将[first1,last1) 范围内的元素与从first2 开始的范围内的元素按顺序进行比较,并返回第一个不匹配发生的位置。
一些代码:
string
mismatch_string( string const & a, string const & b ) {
string::const_iterator longBegin, longEnd, shortBegin;
if( a.length() >= b.length() ) {
longBegin = a.begin();
longEnd = a.end();
shortBegin = b.begin();
}
else {
longBegin = b.begin();
longEnd = b.end();
shortBegin = a.begin();
}
pair< string::const_iterator, string::const_iterator > mismatch_pair =
mismatch( longBegin, longEnd, shortBegin );
return string( mismatch_pair.first, longEnd );
}
将full example with output 上传到键盘。