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++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。