STL_算法_区间的比較(equal、mismatch、 lexicographical_compare)
生活随笔
收集整理的這篇文章主要介紹了
STL_算法_区间的比較(equal、mismatch、 lexicographical_compare)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C++ Primer 學習中。。
。
?
簡單記錄下我的學習過程?(代碼為主)
//全部容器適用
equal(b,e,b2) ? ? ? //用來比較第一個容器[b,e)和第二個容器b2開頭,是否相等
equal(b,e,b2,p)
mismatch(b,e,b2) ? ?//用來查找兩個容器中第一個不相等的數據,返回迭代器
mismatch(b,e,b2,p)
lexicographical_compare(b,e,b2,e2) ? ? ?//用來比較第一個區間是否比第二個區間小
lexicographical_compare(b,e,b2,e2,p)
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<list> #include<algorithm> using namespace std;/***************************************** //全部容器適用 equal(b,e,b2) //用來比較第一個容器[b,e)和第二個容器b2開頭。是否相等 equal(b,e,b2,p) mismatch(b,e,b2) //用來查找兩個容器中第一個不相等的數據,返回迭代器 mismatch(b,e,b2,p) lexicographical_compare(b,e,b2,e2) //用來比較第一個區間是否比第二個區間小 lexicographical_compare(b,e,b2,e2,p) *****************************************//************************************************************************************* std::equal 全部排序容器適用 algorithm -------------------------------------------------------------------------------------- template <class InputIterator1, class InputIterator2>bool equal ( InputIterator1 first1, InputIterator1 last1,InputIterator2 first2 );template <class InputIterator1, class InputIterator2, class BinaryPredicate>bool equal ( InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, BinaryPredicate pred );//eg: template <class InputIterator1, class InputIterator2>bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 ) {while ( first1!=last1 ){if (!(*first1 == *first2)) // or: if (!pred(*first1,*first2)), for pred versionreturn false;++first1; ++first2;}return true; } *************************************************************************************//************************************************************************************* std::mismatch 全部排序容器適用 algorithm -------------------------------------------------------------------------------------- template <class InputIterator1, class InputIterator2>pair<InputIterator1, InputIterator2>mismatch (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2 );template <class InputIterator1, class InputIterator2, class BinaryPredicate>pair<InputIterator1, InputIterator2>mismatch (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, BinaryPredicate pred );//eg: template <class InputIterator1, class InputIterator2>pair<InputIterator1, InputIterator2>mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 ) {while ( (first1!=last1) && (*first1==*first2) ) // or: pred(*first1,*first2), for the pred version{ ++first1; ++first2; }return make_pair(first1,first2); } *************************************************************************************/ /************************************************************************************* std::lexicographical_compare 全部排序容器適用 algorithm -------------------------------------------------------------------------------------- template <class InputIterator1, class InputIterator2>bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2 );template <class InputIterator1, class InputIterator2, class Compare>bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,Compare comp ); //eg: template <class InputIterator1, class InputIterator2>bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2 ) {while (first1!=last1){if (first2==last2 || *first2<*first1) return false;else if (*first1<*first2) return true;first1++; first2++;}return (first2!=last2); } *************************************************************************************/bool mypredicate (int i, int j) {return (i==j); }bool mycomp (char c1, char c2) {return tolower(c1)<tolower(c2); }int main() {//equal(b,e,b2) //用來比較第一個容器[b,e)和第二個容器b2開頭,是否相等//equal(b,e,b2,p)int myints[] = {20,40,60,80,100}; // myints: 20 40 60 80 100vector<int>myvector (myints,myints+5); // myvector: 20 40 60 80 100// using default comparison:if (equal (myvector.begin(), myvector.end(), myints))cout << "The contents of both sequences are equal." << endl;elsecout << "The contents of both sequences differ." << endl;myvector[3]=81; // myvector: 20 40 60 81 100// using predicate comparison:if (equal (myvector.begin(), myvector.end(), myints, mypredicate))cout << "The contents of both sequences are equal." << endl;elsecout << "The contents of both sequences differ." << endl;cout<<endl;/**-----------------------------------------------------------------------------Output:The contents of both sequences are equal.The contents of both sequences differ.-----------------------------------------------------------------------------**///mismatch(b,e,b2) //用來查找兩個容器中第一個不相等的數據,返回迭代器//mismatch(b,e,b2,p)list<int> mylist;for (int i=1; i<6; i++) mylist.push_back (i*10); // mylist: 10 20 30 40 50int myints2[] = {10,20,80,40,1024}; // myints2: 10 20 80 40 1024pair<list<int>::iterator,int*> mypair;// using default comparison:mypair = mismatch (mylist.begin(), mylist.end(), myints2);cout << "First mismatching elements: " << *mypair.first;cout << " and " << *mypair.second << endl;mypair.first++;mypair.second++;// using predicate comparison:mypair = mismatch (mypair.first, mylist.end(), mypair.second, mypredicate);cout << "Second mismatching elements: " << *mypair.first;cout << " and " << *mypair.second << endl;cout <<endl;/**-----------------------------------------------------------------------------Output:First mismatching elements: 30 and 80Second mismatching elements: 50 and 1024-----------------------------------------------------------------------------**///lexicographical_compare(b,e,b2,e2) //用來比較第一個區間是否比第二個區間小 (長度和ASCII碼)//lexicographical_compare(b,e,b2,e2,p)char first[]="apple"; // 5 letterschar second[]="apart"; // 5 letters eg:apartmentcout << "Using default comparison (operator<): ";if (lexicographical_compare(first,first+5,second,second+5))cout << first << " is less than " << second << endl;else if (lexicographical_compare(second,second+5,first,first+5))cout << first << " is greater than " << second << endl;elsecout << first << " and " << second << " are equivalent\n";cout << "Using mycomp as comparison object: ";if (lexicographical_compare(first,first+5,second,second+5,mycomp))cout << first << " is less than " << second << endl;else if (lexicographical_compare(second,second+5,first,first+5,mycomp))cout << first << " is greater than " << second << endl;elsecout << first << " and " << second << " are equivalent\n";/**-----------------------------------------------------------------------------Output:Using default comparison (operator<): apple is greater than apartUsing mycomp as comparison object: apple is greater than apart-----------------------------------------------------------------------------**/return 0; }
轉載于:https://www.cnblogs.com/cxchanpin/p/7289702.html
總結
以上是生活随笔為你收集整理的STL_算法_区间的比較(equal、mismatch、 lexicographical_compare)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu16.04安装使用wineq
- 下一篇: mysql计算用户平均下单周期