// Using the mismatch() algorithm #include <iostream> // For standard streams #include <vector> // For vector container #include <algorithm> // For equal() algorithm #include <string> // For string class #include <iterator> // For stream iterators using std::string; using word_iter = std::vector<string>::iterator; int main() { std::vector<string> words1 {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; std::vector<string> words2 {"two", "three", "four", "five", "six", "eleven", "eight", "nine", "ten"}; auto iter1 = std::begin(words1); auto end_iter1 = std::end(words1); auto iter2 = std::begin(words2); auto end_iter2 = std::end(words2); // Lambda expression to output mismatch() result auto print_match = [](const std::pair<word_iter, word_iter>& pr, const word_iter& end_iter) { if(pr.first != end_iter) std::cout << "\nFirst pair of words that differ are "<< *pr.first << " and " << *pr.second << std::endl; else std::cout << "\nRanges are identical." << std::endl; }; std::cout << "Container - words1: "; std::copy(iter1, end_iter1, std::ostream_iterator<string>{std::cout, " "}); std::cout << "\nContainer - words2: "; std::copy(iter2, end_iter2, std::ostream_iterator<string>{std::cout, " "}); std::cout << std::endl; std::cout << "\nCompare from words1[1] to end with words2:"; print_match(std::mismatch(iter1 + 1, end_iter1, iter2), end_iter1); std::cout << "\nCompare from words2[0] to second-to-last with words1:"; print_match(std::mismatch(iter2, end_iter2 - 1, iter1), end_iter2 - 1); std::cout << "\nCompare from words1[1] to words1[5] with words2:"; print_match(std::mismatch(iter1 + 1, iter1 + 6, iter2), iter1 + 6); std::cout << "\nCompare first 6 from words1 with first 6 in words2:"; print_match(std::mismatch(iter1, iter1 + 6, iter2, iter2 + 6), iter1 + 6); std::cout << "\nCompare all words1 with words2:"; print_match(std::mismatch(iter1, end_iter1, iter2), end_iter1); std::cout << "\nCompare all of words2 with all of words1:"; print_match(std::mismatch(iter2, end_iter2, iter1, end_iter1), end_iter2); std::cout << "\nCompare from words1[1] to end with words2[0] to second-to-last:"; print_match(std::mismatch(iter1 + 1, end_iter1, iter2, end_iter2 - 1), end_iter1); }注意 words2 中的内容和前面示例中的有些不同。每一次应用 mismatch() 的结果都是由定义为 print_match 的 lambda 表达式生成的。它的参数是一个 pair 对象和一个 vector<string> 容器的迭代器。使用 using 指令生成 word_iter 别名可以使 lambda 表达式的定义更简单。
Container - words1: one two three four five six seven eight nine
Container - words2: two three four five six eleven eight nine ten
Compare from words1[1] to end with words2:
First pair of words that differ are seven and eleven
Compare from words2[0] to second-to-last with words1:
First pair of words that differ are two and one
Compare from words1[1] to words1[5] with words2:
Ranges are identical.
Compare first 6 from words1 with first 6 in words2:
First pair of words that differ are one and two
Compare all words1 with words2:
First pair of words that differ are one and two
Compare all of words2 with all of words1:
First pair of words that differ are two and one
Compare from words1[1] to end with words2[0] to second-to-last:
First pair of words that differ are seven and eleven
std::vector<string> range1 {"one", "three", "five", "ten"}; std::vector<string> range2 {"nine", "five", "eighteen”,"seven"}; auto pr = std::mismatch( std::begin(range1), std::end(range1),std:rbegin(range2), std::end(range2),[](const string& s1, const string& s2) { return s1.back() = s2.back(); }); if(pr.first == std::end(range1) || pr.second == std::end(range2)) std::cout << "The ranges are identical." << std::endl; else std::cout << *pr.first << " is not equal to " << *pr.second <<std::endl;当两个字符串的最后一个字符相等时,这个比较会返回 true,所以这段代码的输出为:
five is not equal to eighteen
当然,这是正确的,而且根据比较函数,“one”等于“nine”,“three”等于“five”。Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有