• <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

            靜以修身,儉以養德,非澹薄無以明志,非寧靜無以致遠。
            隨筆 - 397, 文章 - 0, 評論 - 196, 引用 - 0
            數據加載中……

            STL算法(Algorithms):合并(Merge)

            1、merge:將兩個序列合并成一個新的序列,并對新的序列排序
            原型:
            template <class InputIterator1, class InputIterator2, class OutputIterator>
              OutputIterator merge ( InputIterator1 first1, InputIterator1 last1,
                                     InputIterator2 first2, InputIterator2 last2,OutputIterator result );
            template <class InputIterator1, class InputIterator2,class OutputIterator, class Compare>
            OutputIterator merge ( InputIterator1 first1, InputIterator1 last1,
                                     InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp );
            示例:

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

            int main () {
              
            int first[] = {5,10,15,20,25};
              
            int second[] = {50,40,30,20,10};
              vector
            <int> v(10);
              vector
            <int>::iterator it;

              sort (first,first
            +5);
              sort (second,second
            +5);
              merge (first,first
            +5,second,second+5,v.begin());

              cout 
            << "The resulting vector contains:";
              
            for (it=v.begin(); it!=v.end(); ++it)
                cout 
            << " " << *it;

              cout 
            << endl;
              
              
            return 0;
            }



            2、inplace_merge:將兩個序列合并成一個新的序列,并對新的序列進行歸并排序(這兩個序列必須要進過排序)
            原型:
            template <class BidirectionalIterator>
            void inplace_merge ( BidirectionalIterator first, BidirectionalIterator middle,
                                   BidirectionalIterator last );

            template <class BidirectionalIterator, class Compare>
             void inplace_merge ( BidirectionalIterator first, BidirectionalIterator middle,
                                   BidirectionalIterator last, Compare comp );
            示例:

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

            int main () {
              
            int first[] = {5,10,15,20,25};
              
            int second[] = {50,40,30,20,10};
              vector
            <int> v(10);
              vector
            <int>::iterator it;

              sort (first,first
            +5);
              sort (second,second
            +5);

              copy (first,first
            +5,v.begin());
              copy (second,second
            +5,v.begin()+5);

              inplace_merge (v.begin(),v.begin()
            +5,v.end());

              cout 
            << "The resulting vector contains:";
              
            for (it=v.begin(); it!=v.end(); ++it)
                cout 
            << " " << *it;

              cout 
            << endl;
              
              
            return 0;
            }


             

            3、includes:測試是一個序列是否在另一個序列中
            原型:
            template <class InputIterator1, class InputIterator2>
            bool includes ( InputIterator1 first1, InputIterator1 last1,
                            InputIterator2 first2, InputIterator2 last2 );
            template <class InputIterator1, class InputIterator2, class Compare>
            bool includes ( InputIterator1 first1, InputIterator1 last1,
                            InputIterator2 first2, InputIterator2 last2, Compare comp );
            示例:

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

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

            int main () {
              
            int container[] = {5,10,15,20,25,30,35,40,45,50};
              
            int continent[] = {40,30,20,10};

              sort (container,container
            +10);
              sort (continent,continent
            +4);

              
            // using default comparison:
              if ( includes(container,container+10,continent,continent+4) )
                cout 
            << "container includes continent!" << endl;

              
            // using myfunction as comp:
              if ( includes(container,container+10,continent,continent+4, myfunction) )
                cout 
            << "container includes continent!" << endl;

              
            return 0;
            }


             

            4、set_union:和merge類似,不過新序列中沒有重復的元素
            原型:
            template <class InputIterator1, class InputIterator2, class OutputIterator> 
            OutputIterator set_union ( InputIterator1 first1, InputIterator1 last1,         
                                       InputIterator2 first2, InputIterator2 last2,OutputIterator result );
            template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> 
            OutputIterator set_union ( InputIterator1 first1, InputIterator1 last1,
                                       InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp );
            示例:

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

            int main () {
              
            int first[] = {5,10,15,20,25};
              
            int second[] = {50,40,30,20,10};
              vector
            <int> v(10);                           // 0  0  0  0  0  0  0  0  0  0
              vector<int>::iterator it;

              sort (first,first
            +5);     //  5 10 15 20 25
              sort (second,second+5);   // 10 20 30 40 50

              it
            =set_union (first, first+5, second, second+5, v.begin());
                                                           
            // 5 10 15 20 25 30 40 50  0  0

              cout 
            << "union has " << int(it - v.begin()) << " elements.\n";

              
            return 0;
            }


             

            5、set_intersection:兩個序列的交集
            原型:
            template <class InputIterator1, class InputIterator2, class OutputIterator> 
            OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
                                              InputIterator2 first2, InputIterator2 last2,OutputIterator result );
            template <class InputIterator1, class InputIterator2,class OutputIterator, class Compare>
            OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
                                              InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp );
            示例:

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

            int main () {
              
            int first[] = {5,10,15,20,25};
              
            int second[] = {50,40,30,20,10};
              vector
            <int> v(10);                           // 0  0  0  0  0  0  0  0  0  0
              vector<int>::iterator it;

              sort (first,first
            +5);     //  5 10 15 20 25
              sort (second,second+5);   // 10 20 30 40 50

              it
            =set_intersection (first, first+5, second, second+5, v.begin());
                                                           
            // 10 20 0  0  0  0  0  0  0  0

              cout 
            << "intersection has " << int(it - v.begin()) << " elements.\n";

              
            return 0;
            }


             

            6、set_difference:序列(first1,last1)不在序列(first2,last2)中的元素
            原型:
            template <class InputIterator1, class InputIterator2, class OutputIterator> 
            OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
                  InputIterator2 first2, InputIterator2 last2,outputIterator result );
            template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> 
            OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
                                              InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp );
            示例:

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

            int main () {
              
            int first[] = {5,10,15,20,25};
              
            int second[] = {50,40,30,20,10};
              vector
            <int> v(10);                           // 0  0  0  0  0  0  0  0  0  0
              vector<int>::iterator it;

              sort (first,first
            +5);     //  5 10 15 20 25
              sort (second,second+5);   // 10 20 30 40 50

              it
            =set_difference (first, first+5, second, second+5, v.begin());
                                                           
            // 5 15 25  0  0  0  0  0  0  0

              cout 
            << "difference has " << int(it - v.begin()) << " elements.\n";

              
            return 0;
            }


             

            7、set_symmetric_difference:所有不在序列(first1,last1)和序列(first2,last2)中的元素
            原型:
            template <class InputIterator1, class InputIterator2, class OutputIterator> 
            OutputIterator    set_symmetric_difference ( InputIterator1 first1, InputIterator1 last1,
                                                         InputIterator2 first2, InputIterator2 last2,OutputIterator result );
            template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
            OutputIterator    set_symmetric_difference ( InputIterator1 first1, InputIterator1 last1,
                                                         InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp );
            示例:

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

            int main () {
              
            int first[] = {5,10,15,20,25};
              
            int second[] = {50,40,30,20,10};
              vector
            <int> v(10);                           // 0  0  0  0  0  0  0  0  0  0
              vector<int>::iterator it;

              sort (first,first
            +5);     //  5 10 15 20 25
              sort (second,second+5);   // 10 20 30 40 50

              it
            =set_symmetric_difference (first, first+5, second, second+5, v.begin());
                                                           
            // 5 15 25 30 40 50  0  0  0  0

              cout 
            << "symmetric difference has " << int(it - v.begin()) << " elements.\n";

              
            return 0;
            }


            posted on 2012-02-05 21:33 Benjamin 閱讀(10135) 評論(0)  編輯 收藏 引用 所屬分類: 泛型編程

            国内精品伊人久久久久777| 久久久噜噜噜久久熟女AA片| 精品九九久久国内精品| 91久久精一区二区三区大全| 日韩精品久久久久久| 久久久青草青青国产亚洲免观| 婷婷久久综合九色综合九七| 久久精品国产亚洲av影院| 国产精品永久久久久久久久久| 久久免费视频1| 91精品久久久久久无码| 亚洲AⅤ优女AV综合久久久| 久久99热精品| 一本色道久久99一综合| 久久99精品久久久久久不卡| 久久综合久久自在自线精品自| 国产精品久久久久一区二区三区| 久久精品一区二区三区AV| 国产亚洲精久久久久久无码AV| 久久棈精品久久久久久噜噜| 天天综合久久一二三区| 色偷偷888欧美精品久久久| 亚洲AV日韩精品久久久久| 午夜精品久久久内射近拍高清 | 国产高潮国产高潮久久久91| 久久成人国产精品免费软件| 久久精品国产亚洲7777| 一本大道久久a久久精品综合| 久久精品一本到99热免费| 囯产极品美女高潮无套久久久| 久久一区二区三区99| 久久国产精品波多野结衣AV| 久久99精品免费一区二区| 伊人久久大香线蕉精品| 久久久久国产一级毛片高清版| 久久国产精品国产自线拍免费| 久久精品国产久精国产果冻传媒| 欧美久久久久久精选9999| 久久艹国产| 亚洲欧美一级久久精品| 伊人久久大香线蕉精品不卡|