For C++20, is there a library equivalent of Java's StringUtils.indexOfDifference()
?
What is the efficient way of comparing two large strings and determining where they differ?
You can use std::mismatch
from <algorithm>
Returns the first mismatching pair of elements from two ranges
For example
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
std::size_t IndexOfDifference(std::string const& lhs, std::string const& rhs) {
auto diff = std::mismatch(lhs.begin(), lhs.end(), rhs.begin());
return std::distance(lhs.begin(), diff.first);
}
int main() {
std::cout << IndexOfDifference("foobar", "foocar");
}
Will output
3