• <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)。
            隨筆 - 398, 文章 - 0, 評論 - 196, 引用 - 0
            數(shù)據(jù)加載中……

            STL算法(Algorithms):排序

            一、sort:對一定范圍內(nèi)的所有元素排序

            原型:

            template <class RandomAccessIterator>

              void sort ( RandomAccessIterator first, RandomAccessIterator last );

            template <class RandomAccessIterator, class Compare>

              void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
            例子代碼:

            // sort algorithm example

            #include <iostream>

            #include <algorithm>

            #include <vector>

            using namespace std;

             

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

             

            struct myclass {

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

            } myobject;

             

            int main () {

              int myints[] = {32,71,12,45,26,80,53,33};

              vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33

              vector<int>::iterator it;

             

              // using default comparison (operator <):

              sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33

             

              // using function as comp

              sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

             

              // using object as comp

              sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)

             

              // print out content:

              cout << "myvector contains:";

              for (it=myvector.begin(); it!=myvector.end(); ++it)

                cout << " " << *it;

             

              cout << endl;

             

              return 0;

            }

            二、stable_sort:歸并排序

            原型:

            template <class RandomAccessIterator>

              void stable_sort ( RandomAccessIterator first, RandomAccessIterator last );

            template <class RandomAccessIterator, class Compare>

              void stable_sort ( RandomAccessIterator first, RandomAccessIterator last,

                                 Compare comp );
            例子代碼:

            // stable_sort example

            #include <iostream>

            #include <algorithm>

            #include <vector>

            using namespace std;

             

            bool compare_as_ints (double i,double j)

            {

              return (int(i)<int(j));

            }

             

            int main () {

              double mydoubles[] = {3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58};

             

              vector<double> myvector;

              vector<double>::iterator it;

             

              myvector.assign(mydoubles,mydoubles+8);

             

              cout << "using default comparison:";

              stable_sort (myvector.begin(), myvector.end());

              for (it=myvector.begin(); it!=myvector.end(); ++it)

                cout << " " << *it;

             

              myvector.assign(mydoubles,mydoubles+8);

             

              cout << "\nusing 'compare_as_ints' :";

              stable_sort (myvector.begin(), myvector.end(), compare_as_ints);

              for (it=myvector.begin(); it!=myvector.end(); ++it)

                cout << " " << *it;

             

              cout << endl;

             

              return 0;

            }

            三、partial_sort_copy:對序列中的某個范圍的元素進(jìn)行排序(部分排序)

            原型:

            template <class InputIterator, class RandomAccessIterator>

              RandomAccessIterator

                partial_sort_copy ( InputIterator first,InputIterator last,

                                    RandomAccessIterator result_first,

                                    RandomAccessIterator result_last );

            template <class InputIterator, class RandomAccessIterator, class Compare>

              RandomAccessIterator

                partial_sort_copy ( InputIterator first,InputIterator last,

                                    RandomAccessIterator result_first,

                                    RandomAccessIterator result_last, Compare comp );
            示例代碼:

            // partial_sort example
            #include <iostream>
            #include <algorithm>
            #include <vector>
            using namespace std;

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

            int main () {
              int myints[] = {9,8,7,6,5,4,3,2,1};
              vector<int> myvector (myints, myints+9);
              vector<int>::iterator it;

              // using default comparison (operator <):
              partial_sort (myvector.begin(), myvector.begin()+5, myvector.end());

              // using function as comp
              partial_sort (myvector.begin(), myvector.begin()+5, myvector.end(),myfunction);

              // print out content:
              cout << "myvector contains:";
              for (it=myvector.begin(); it!=myvector.end(); ++it)
                cout << " " << *it;

              cout << endl;

              return 0;
            }

            四、partial_sort_copy部分排序后拷貝
            原型:

            template <class InputIterator, class RandomAccessIterator>

              RandomAccessIterator

                partial_sort_copy ( InputIterator first,InputIterator last,

                                    RandomAccessIterator result_first,

                                    RandomAccessIterator result_last );

            template <class InputIterator, class RandomAccessIterator, class Compare>

              RandomAccessIterator

                partial_sort_copy ( InputIterator first,InputIterator last,

                                    RandomAccessIterator result_first,

                                    RandomAccessIterator result_last, Compare comp );

            示例:

            // partial_sort_copy example
            #include <iostream>
            #include <algorithm>
            #include <vector>
            using namespace std;
            
            bool myfunction (int i,int j) { return (i<j); }
            
            int main () {
              int myints[] = {9,8,7,6,5,4,3,2,1};
              vector<int> myvector (5);
              vector<int>::iterator it;
            
              // using default comparison (operator <):
              partial_sort_copy (myints, myints+9, myvector.begin(), myvector.end());
            
              // using function as comp
              partial_sort_copy (myints, myints+9, myvector.begin(), myvector.end(), myfunction);
            
              // print out content:
              cout << "myvector contains:";
              for (it=myvector.begin(); it!=myvector.end(); ++it)
                cout << " " << *it;
            
              cout << endl;
            
              return 0;
            }
            五、nth_element:使第n個元素處在序列中的第n個位置,并在這個元素前都是比這個元素小的,
            這個元素后面都是比這個這元素大,但是這前后兩個序列并不一定都是有序的
            (排過序);
             原型:

            template <class RandomAccessIterator>

              void nth_element ( RandomAccessIterator first, RandomAccessIterator nth,

                                 RandomAccessIterator last );

             

            template <class RandomAccessIterator, class Compare>

              void nth_element ( RandomAccessIterator first, RandomAccessIterator nth,

                                 RandomAccessIterator last, Compare comp );

            例子代碼:
            // nth_element example
            #include <iostream>
            #include <algorithm>
            #include <vector>
            using namespace std;
            
            bool myfunction (int i,int j) { return (i<j); }
            
            int main () {
              vector<int> myvector;
              vector<int>::iterator it;
            
              // set some values:
              for (int i=1; i<10; i++) myvector.push_back(i);   // 1 2 3 4 5 6 7 8 9
            
              random_shuffle (myvector.begin(), myvector.end());
            
              // using default comparison (operator <):
              nth_element (myvector.begin(), myvector.begin()+5, myvector.end());
            
              // using function as comp
              nth_element (myvector.begin(), myvector.begin()+5, myvector.end(),myfunction);
            
              // print out content:
              cout << "myvector contains:";
              for (it=myvector.begin(); it!=myvector.end(); ++it)
                cout << " " << *it;
            
              cout << endl;
            
              return 0;
            }




             

            posted on 2012-01-07 16:32 Benjamin 閱讀(520) 評論(0)  編輯 收藏 引用 所屬分類: 泛型編程

            亚洲午夜精品久久久久久浪潮 | 亚洲国产精品无码久久青草| 中文字幕亚洲综合久久2| 久久久久亚洲AV成人网| 久久精品国产亚洲AV不卡| 久久国产精品成人免费| 精品无码久久久久久久久久 | 精品久久久久久国产| 久久免费的精品国产V∧| 久久五月精品中文字幕| 久久亚洲AV成人无码国产| 国内精品伊人久久久久影院对白| 一本久久a久久精品vr综合| 国产一区二区精品久久凹凸 | 久久久久国产日韩精品网站 | 人妻无码久久精品| 精品久久久久久国产91| 久久精品国产男包| 伊色综合久久之综合久久| 伊人久久综在合线亚洲2019| 精品久久8x国产免费观看| 亚洲欧洲久久久精品| 精品久久久久久国产牛牛app| 免费精品久久天干天干| 日日狠狠久久偷偷色综合免费| 国产∨亚洲V天堂无码久久久| 伊人 久久 精品| 亚洲精品乱码久久久久久蜜桃 | 久久精品99久久香蕉国产色戒 | 久久精品国产亚洲AV忘忧草18| 国产精品伊人久久伊人电影| 久久99国产精品99久久| 久久精品国产91久久综合麻豆自制 | 日韩精品久久久久久| 东京热TOKYO综合久久精品| 国产成年无码久久久久毛片| 久久无码人妻一区二区三区午夜 | 久久只这里是精品66| 中文字幕无码av激情不卡久久| 伊人久久大香线蕉成人| 精品人妻伦九区久久AAA片69|