• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Benjamin

            靜以修身,儉以養(yǎng)德,非澹薄無以明志,非寧靜無以致遠(yuǎn)。
            隨筆 - 397, 文章 - 0, 評論 - 196, 引用 - 0
            數(shù)據(jù)加載中……

            STL算法(Algorithms):極值

            1、min:返回兩個兩個參數(shù)中的最小值
            原型:template <class T> const T& min ( const T& a, const T& b );
            template <class T, class Compare>
              const T& min ( const T& a, const T& b, Compare comp );
            示例:
            // min example
            #include <iostream>
            #include <algorithm>
            using namespace std;

            int main () {
            cout << "min(1,2)==" << min(1,2) << endl;
              cout << "min(2,1)==" << min(2,1) << endl;
              cout << "min('a','z')==" << min('a','z') << endl;
              cout << "min(3.14,2.72)==" << min(3.14,2.72) << endl;
              return 0;
            }
            2、max:返回兩個參數(shù)中的大值
            原型:
            template <class T> const T& max ( const T& a, const T& b );
            template <class T, class Compare>
              const T& max ( const T& a, const T& b, Compare comp );
            示例:
            // max example
            #include <iostream>
            #include <algorithm>
            using namespace std;

            int main () { cout << "max(1,2)==" << max(1,2) << endl;
              cout << "max(2,1)==" << max(2,1) << endl;
              cout << "max('a','z')==" << max('a','z') << endl;
              cout << "max(3.14,2.73)==" << max(3.14,2.73) << endl;
              return 0;
            }
            3、min_element:返回(迭代器)指定范圍內(nèi)的最小元素
            原型:
            // min_element/max_element
            #include <iostream>
            #include <algorithm>
            using namespace std;

            bool myfn(int i, int j) { return i<j; }

            struct myclass {
              bool operator() (int i,int j) { return i<j; }
            } myobj;

            int main () {
              int myints[] = {3,7,2,5,6,4,9};

              // using default comparison:
              cout << "The smallest element is " << *min_element(myints,myints+7) << endl;
              cout << "The largest element is " << *max_element(myints,myints+7) << endl;

              // using function myfn as comp:
              cout << "The smallest element is " << *min_element(myints,myints+7,myfn) << endl;
              cout << "The largest element is " << *max_element(myints,myints+7,myfn) << endl;

              // using object myobj as comp:
              cout << "The smallest element is " << *min_element(myints,myints+7,myobj) << endl; cout << "The largest element is " << *max_element(myints,myints+7,myobj) << endl;

              return 0;
            }
            4、max_element:返回(迭代器)指定范圍內(nèi)的最小元素
            原型:
            template <class ForwardIterator>
              ForwardIterator max_element ( ForwardIterator first, ForwardIterator last );

            template <class ForwardIterator, class Compare>
              ForwardIterator max_element ( ForwardIterator first, ForwardIterator last,
                                            Compare comp );
            示例:
            // min_element/max_element
            #include <iostream>
            #include <algorithm>
            using namespace std;

            bool myfn(int i, int j) { return i<j; }

            struct myclass {
              bool operator() (int i,int j) { return i<j; }
            } myobj;

            int main () {
              int myints[] = {3,7,2,5,6,4,9};

              // using default comparison:
              cout << "The smallest element is " << *min_element(myints,myints+7) << endl;
              cout << "The largest element is " << *max_element(myints,myints+7) << endl;

              // using function myfn as comp:
              cout << "The smallest element is " << *min_element(myints,myints+7,myfn) << endl;
              cout << "The largest element is " << *max_element(myints,myints+7,myfn) << endl;

              // using object myobj as comp:
              cout << "The smallest element is " << *min_element(myints,myints+7,myobj) << endl; cout << "The largest element is " << *max_element(myints,myints+7,myobj) << endl;

              return 0;
            }
            5、next_permutation:返回的是(序列中所有元素組合中的)一個
            原型:
            template <class BidirectionalIterator>
              bool next_permutation (BidirectionalIterator first,
                                     BidirectionalIterator last );

            template <class BidirectionalIterator, class Compare>
              bool next_permutation (BidirectionalIterator first,
                                     BidirectionalIterator last, Compare comp);
            示例:
            // next_permutation
            #include <iostream>
            #include <algorithm>
            using namespace std;
            
            int main () {
              int myints[] = {1,2,3};
            
              cout << "The 3! possible permutations with 3 elements:\n";
            
              sort (myints,myints+3);
            
              do {
                cout << myints[0] << " " << myints[1] << " " << myints[2] << endl;
              } while ( next_permutation (myints,myints+3) );
            
              return 0;
            }
            6、prev_permutation:和next_permutation功能類似,返回的是序列中(所有元素)前一個組合
            原型:
            <algorithm>
            template <class BidirectionalIterator>
              bool prev_permutation (BidirectionalIterator first,
                                     BidirectionalIterator last );
            
            template <class BidirectionalIterator, class Compare>
              bool prev_permutation (BidirectionalIterator first,
                                     BidirectionalIterator last, Compare comp);
            示例:
            // prev_permutation
            #include <iostream>
            #include <algorithm>
            using namespace std;
            
            int main () {
              int myints[] = {1,2,3};
            
              cout << "The 3! possible permutations with 3 elements:\n";
            
              sort (myints,myints+3);
              reverse (myints,myints+3);
            
              do {
                cout << myints[0] << " " << myints[1] << " " << myints[2] << endl;
              } while ( prev_permutation (myints,myints+3) );
            
              return 0;
            }
            7、lexicographical_compare:字典比較(針對的是兩個序列,返回的是布爾值)
            原型:
            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 );
            示例:// lexicographical_compare example #include <iostream> #include <algorithm> #include <cctype> using namespace std; // a case-insensitive comparison function: bool mycomp (char c1, char c2) { return tolower(c1)<tolower(c2); } int main () { char first[]="Apple"; // 5 letters char second[]="apartment"; // 9 letters cout << "Using default comparison (operator<): "; if (lexicographical_compare(first,first+5,second,second+9)) cout << first << " is less than " << second << endl; else if (lexicographical_compare(second,second+9,first,first+5)) cout << first << " is greater than " << second << endl; else cout << first << " and " << second << " are equivalent\n"; cout << "Using mycomp as comparison object: "; if (lexicographical_compare(first,first+5,second,second+9,mycomp)) cout << first << " is less than " << second << endl; else if (lexicographical_compare(second,second+9,first,first+5,mycomp)) cout << first << " is greater than " << second << endl; else cout << first << " and " << second << " are equivalent\n"; return 0; }

             

            posted on 2012-01-08 16:54 Benjamin 閱讀(860) 評論(1)  編輯 收藏 引用 所屬分類: 泛型編程

            評論

            # re: STL算法(Algorithms):極值[未登錄]  回復(fù)  更多評論   

            非淡泊無以明志
            2012-01-09 13:26 | 春秋十二月
            亚洲国产成人久久综合野外| 国产欧美一区二区久久| 欧美日韩中文字幕久久伊人| 中文字幕无码免费久久| 亚洲а∨天堂久久精品| 久久亚洲天堂| 国产精品美女久久福利网站| 亚洲精品综合久久| 久久精品人妻中文系列| 亚洲色欲久久久综合网| 久久人人爽人人爽人人片AV不 | 亚洲AV日韩精品久久久久久 | 久久久久亚洲精品日久生情| 婷婷国产天堂久久综合五月| 亚洲精品午夜国产va久久| 中文精品99久久国产| 亚洲乱码中文字幕久久孕妇黑人 | 草草久久久无码国产专区| 国产亚洲美女精品久久久| 日韩影院久久| 亚洲精品乱码久久久久久蜜桃不卡 | 青青青青久久精品国产h久久精品五福影院1421 | 精品无码久久久久久国产| 久久婷婷人人澡人人| 精品久久久无码21p发布| 国内精品久久久久久久97牛牛 | 蜜桃麻豆www久久| 欧美性大战久久久久久| 日本强好片久久久久久AAA| 日本久久久精品中文字幕| 久久综合成人网| 国内精品久久久久伊人av| 久久久久久国产a免费观看不卡| 久久精品一本到99热免费| 97久久精品国产精品青草| 日韩久久久久中文字幕人妻| 精品久久久久香蕉网| 久久人妻少妇嫩草AV蜜桃| 久久久久四虎国产精品| 精品国产乱码久久久久软件| 久久久久亚洲爆乳少妇无|