boost - C++ compare strings up to "%" char -
i implement string comparison in c++ comparing strings "%" sign.
i this:
std::equal(str1.begin(), std::find(str1.begin(), str1.end(), l'%'), str2.begin());
since i'm doing in loop on many strings, wonder if there method without 2 distinct string traversals find
, equal
(maybe predicate can abort comparison @ point). boost ok.
you can try std::mismatch
.
the following code run c++14 (it requires template overload 2 iterator pairs), works quite similar in c++11 (or 03, without lambdas though):
auto iters = std::mismatch( str1.begin(), str1.end(), str2.begin(), str2.end(), [] (char lhs, char rhs) {return lhs != '%' && lhs == rhs;}); if (iters.first == str1.end() || iters.second == str2.end() || *iters.first == '%') // success […]
demo.
Comments
Post a Comment