C++ STL具有许多有用的功能,可以帮助我们完成各种编程任务。这样的函数就是“ mismatch() ”。此函数在“ algorithm ”头文件中定义,有助于比较2个容器的不匹配情况。此函数有2个版本。两者都将在本文中讨论。
- mismatch(start_iter1,end_iter1,start_iter2)此版本的不匹配仅测试不平等。
这里有3个参数,
start_iter1:开始迭代器到第一个容器
end_iter1:最后一个迭代器到第一个容器
start_iter2:从开始比较器的第二个迭代器开始迭代。此函数返回第一个不匹配对指针,第一个元素指向第一个容器的第一个不匹配元素的位置,第二个元素指向第二个容器的第一个不匹配元素的位置。如果未找到不匹配项,则第一个元素指向第一个容器的最后一个元素之后的位置,第二个元素指向第二个容器中的相应位置。
// C++ code to demonstrate the working of // mismatch( start_iter1, end_iter1, start_iter2 ) #include
#include #include using namespace std; int main() { // initializing vectors vector v1 = { 1, 10, 15, 20 }; vector v2 = { 1, 10, 25, 30, 45 }; vector v3 = { 1, 10, 15, 20 }; vector v4 = { 1, 10, 15, 20, 24 }; // declaring pointer pair pair< vector ::iterator, vector ::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
- mismatch(start_iter1,end_iter1,start_iter2,比较器):此函数几乎与上述版本相似,但它不仅可以提供相等性不匹配的功能,还可以通过用户定义的比较器功能查找其他用户定义的和所需的不匹配的函数作为第4个参数发送,并返回布尔值true或false。
// C++ code to demonstrate the working of // mismatch( start_iter1, end_iter1, start_iter2, comparator ) #include
#include #include 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 v1 = { 23, 13, 15, 20 }; vector v2 = { 1, 10, 25, 30, 45 }; vector v3 = { 12, 100, 152, 204 }; vector v4 = { 1, 10, 15, 20, 24 }; // declaring pointer pair pair< vector ::iterator, vector ::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++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。