C++ STL 有很多有用的函数可以帮助我们完成各种编程任务。一个这样的函数是 “mismatch()” 。此函数在 “algorithm” 头文件中定义,有助于比较 2 个容器的不匹配。此函数有 2 个版本。两者都在本文中进行了讨论。
- 不匹配( start_iter1, end_iter1, start_iter2 )这个版本的不匹配只测试不等式。这里有 3 个参数,start_iter1:开始迭代器到第一个 containerend_iter1:最后一个迭代器到第一个 containerstart_iter2:开始迭代器到第二个迭代器,从那里开始比较。
此函数返回第一个不匹配对指针,第一个元素指向第一个容器的第一个不匹配元素的位置,第二个元素指向第二个容器的第一个不匹配元素的位置。如果没有发现不匹配,则第一个元素指向第一个容器的最后一个元素之后的位置,第二个元素指向第二个容器中的相应位置。
// C++ code to demonstrate the working of // mismatch( start_iter1, end_iter1, start_iter2 ) #include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { // initializing vectors vector<int> v1 = { 1, 10, 15, 20 }; vector<int> v2 = { 1, 10, 25, 30, 45 }; vector<int> v3 = { 1, 10, 15, 20 }; vector<int> v4 = { 1, 10, 15, 20, 24 }; // declaring pointer pair pair< vector<int>::iterator, vector<int>::iterator > mispair; // using mismatch() to search for 1st mismatch mispair = mismatch(v1.begin(), v1.end(), v2.begin()); // printing the mismatch pair // 1st mismatch at 15 and 25 cout << "The 1st mismatch element of 1st container:"; cout << *mispair.first << endl; cout << "The 1st mismatch element of 2nd container:"; cout << *mispair.second << endl; // using mismatch() to search for 1st mismatch mispair = mismatch(v3.begin(), v3.end(), v4.begin()); // printing the mismatch pair // no mismatch // points to position after last 0 and corresponding 24 cout << "The returned value from 1st container is:"; cout << *mispair.first << endl; cout << "The returned value from 2nd container is:"; cout << *mispair.second << endl; }
输出:
The 1st mismatch element of 1st container:15 The 1st mismatch element of 2nd container:25 The returned value from 1st container is :0 The returned value from 2nd container is :24
- 不匹配(start_iter1、end_iter1、start_iter2、比较器):此函数几乎与上述版本的工作方式相似,但它不仅提供了查找相等性不匹配的函数,还提供了通过用户定义的比较器函数查找其他用户定义的和期望的不匹配的函数,该比较器函数作为第 4 个参数发送并返回布尔值 true或假。
// C++ code to demonstrate the working of // mismatch( start_iter1, end_iter1, start_iter2, comparator ) #include<iostream> #include<algorithm> #include<vector> using namespace std; // comparator function // returns true when element from // 1st element is greater than 2nd bool compare(int a, int b) { return (a>b); } int main() { // initializing vectors vector<int> v1 = { 23, 13, 15, 20 }; vector<int> v2 = { 1, 10, 25, 30, 45 }; vector<int> v3 = { 12, 100, 152, 204 }; vector<int> v4 = { 1, 10, 15, 20, 24 }; // declaring pointer pair pair< vector<int>::iterator, vector<int>::iterator > mispair; // using mismatch() to search for 1st mismatch mispair = mismatch(v1.begin(), v1.end(), v2.begin(), compare); // printing the mismatch pair // 1st mismatch at 15 and 25 // 15 is 1st element less than 2nd at same position cout << "The 1st mismatch element of 1st container:"; cout << *mispair.first << endl; cout << "The 1st mismatch element of 2nd container:"; cout << *mispair.second << endl; // using mismatch() to search for 1st mismatch mispair = mismatch(v3.begin(), v3.end(), v4.begin(), compare); // printing the mismatch pair // no mismatch // all elements in 1st container are greater than 2nd // points to position after last 0 and corresponding 24 cout << "The returned value from 1st container is :"; cout << *mispair.first << endl; cout << "The returned value from 2nd container is :"; cout << *mispair.second << endl; }
输出:
The 1st mismatch element of 1st container:15 The 1st mismatch element of 2nd container:25 The returned value from 1st container is :0 The returned value from 2nd container is :24
相关用法
- C语言 strtok()、strtok_r()用法及代码示例
- C++ wcscpy()用法及代码示例
- C++ wcscmp()用法及代码示例
- C++ ratio_equal()用法及代码示例
- C++ quick_exit()用法及代码示例
- C++ multiset lower_bound()用法及代码示例
- C++ multiset upper_bound()用法及代码示例
- C++ multiset max_size()用法及代码示例
- C++ forward_list max_size()用法及代码示例
- C++ std::allocator()用法及代码示例
- C++ array data()用法及代码示例
- C++ multiset size()用法及代码示例
- C++ ratio_not_equal()用法及代码示例
- C++ std::bit_or用法及代码示例
- C++ iswprint()用法及代码示例
- C++ iswgraph()用法及代码示例
- C++ btowc()用法及代码示例
- C++ negative_binomial_distribution用法及代码示例
- C++ mbrtoc16()用法及代码示例
- C++ mbrtoc32()用法及代码示例
- C++ wmemset()用法及代码示例
- C++ wcsspn()用法及代码示例
注:本文由纯净天空筛选整理自GeeksforGeeks大神的英文原创作品 std::mismatch() with examples in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。