zoukankan      html  css  js  c++  java
  • STL学习笔记(已序区间算法)

    针对已序区间执行的算法,执行前提是源区间必须在某个排序准则下已序。

    搜寻元素(Searching)

    1.检查某个元素是否存在

    bool

    binary_search(ForwardIterator beg,ForwardIterator end,

                           const T& value)

    bool

    binary_search(ForwardIterator beg,ForwardIterator end,

                           const T& value,

                           BinaryPredicate op)

    以下示范binary_search()的用法

     1 #include "algostuff.hpp"
     2 using namespace std;
     3 
     4 int main()
     5 {
     6     list<int> coll;
     7     INSERT_ELEMENTS(coll,1,9);
     8     PRINT_ELEMENTS(coll);
     9     if(binary_search(coll.begin(),coll.end(),5))
    10         cout<<"5 is present"<<endl;
    11     else
    12         cout<<"5 is not present"<<endl;
    13     if(binary_search(coll.begin(),coll.end(),42))
    14         cout<<"42 is present"<<endl;
    15     else
    16         cout<<"42 is not present"<<endl;
    17 }
    View Code

    2.检查若干值是否存在

    bool

    includes(InputIterator beg,

                  InputIterator end,

                  InputIterator searchBeg,

                  InputIterator searchEnd)

    bool

    includes(InputIterator beg,

                  InputIterator end,

                  InputIterator searchBeg,

                  InputIterator searchEnd,

                  BinaryPredicate op)

    两种形式都用来判断已序区间[beg,end)是否包含另一已序区间[searchBeg,searchEnd)的全部元素

    以下程序示范inlcudes()的用法

     1 #include "algostuff.hpp"
     2 using namespace std;
     3 
     4 int main()
     5 {
     6     list<int> coll;
     7     vector<int> search;
     8     INSERT_ELEMENTS(coll,1,9);
     9     PRINT_ELEMENTS(coll,"coll: ");
    10     search.push_back(3);
    11     search.push_back(4);
    12     search.push_back(7);
    13     PRINT_ELEMENTS(search,"search: ");
    14     if(includes(coll.begin(),coll.end(),search.begin(),search.end()))
    15         cout<<"all elements of search are also in coll"<<endl;
    16     else
    17         cout<<"not all elements of search are also in coll"<<endl;
    18 }
    View Code

    3.搜寻第一个或最后一个可能位置

    ForwardIterator

    lower_bound(ForwardIterator beg,ForwardIterator end,const T& value)

    ForwardIterator

    lower_bound(ForwardIterator beg,ForwardIterator end,const T& value,

                          BinaryPredicate op)

    ForwardIterator

    upper_bound(ForwardIterator beg,ForwardIterator end,const T& value)

    ForwardIterator

    upper_bound(ForwardIterator beg,ForwardIterator end,const T& value,

                          BinaryPredicate op)

    1.lower_bound()返回第一个“大于等于value”的元素位置。

    2.upper_bound()返回第一个“大于value”元素的位置

    以下程序示范lower_bound()和upper_bound()的用法

     1 #include "algostuff.hpp"
     2 using namespace std;
     3 
     4 int main()
     5 {
     6     list<int> coll;
     7     INSERT_ELEMENTS(coll,1,9);
     8     INSERT_ELEMENTS(coll,1,9);
     9     coll.sort();
    10     PRINT_ELEMENTS(coll);
    11     list<int>::iterator pos1,pos2;
    12     pos1=lower_bound(coll.begin(),coll.end(),5);
    13     pos2=upper_bound(coll.begin(),coll.end(),5);
    14     cout<<"5 could get position "
    15         <<distance(coll.begin(),pos1)+1
    16         <<" up to "
    17         <<distance(coll.begin(),pos2)+1
    18         <<" without breaking the sorting"<<endl;
    19     coll.insert(lower_bound(coll.begin(),coll.end(),3),3);
    20     coll.insert(upper_bound(coll.begin(),coll.end(),7),7);
    21     PRINT_ELEMENTS(coll);
    22 }
    View Code

    3.搜寻第一个和最后一个可能位置

    pair<ForwardIterator,ForwardIterator>

    equal_range(ForwardIterator beg,ForwardIterator end,const T& value)

    pair<ForwardIterator,ForwardIterator>

    equal_range(ForwardIterator beg,ForwardIterator end,const T& value,

                         BinaryPredicate op)

    返回值与下式等效: make_pair(lower_bound(...),upper_bound(...))

    以下程序展示equal_range()的用法

     1 #include "algostuff.hpp"
     2 using namespace std;
     3 
     4 bool bothEvenOrOdd(int elem1,int elem2)
     5 {
     6     return elem1%2==elem2%2;
     7 }
     8 
     9 int main()
    10 {
    11     vector<int> coll1;
    12     list<int> coll2;
    13     INSERT_ELEMENTS(coll1,1,7);
    14     INSERT_ELEMENTS(coll2,3,9);
    15     PRINT_ELEMENTS(coll1,"coll1: ");
    16     PRINT_ELEMENTS(coll2,"coll2: ");
    17     if(equal(coll1.begin(),coll1.end(),coll2.begin()))
    18         cout<<"coll1==coll2"<<endl;
    19     else
    20         cout<<"coll1!=coll2"<<endl;
    21     if(equal(coll1.begin(),coll1.end(),coll2.begin(),bothEvenOrOdd))
    22         cout<<"even and odd elements correspond"<<endl;
    23     else
    24         cout<<"even and odd elements do not correspond"<<endl;
    25 }
    View Code

    合并元素(Merging)

    1.两个已序集合的总和

    OutputIterator

    merge(InputIterator source1Beg,InputIterator source1End,

               InputIterator source2Beg,InputIterator source2End,

               OutputIterator destBeg)

    OutputIterator

    merge(InputIterator source1Beg,InputIterator source1End,

               InputIterator source2Beg,InputIterator source2End,

               OutputIterator destBeg,BinaryPredicate op)

    1.两者都是将两个源区间的元素合并,使得“以destBeg起始的目标区间”内含两个源区间所有元素。

    2.目标区间内的所有元素都将按顺序排序

    下面这个例子展示merge()的用法

     1 #include <iterator>
     2 #include "algostuff.hpp"
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     list<int> coll1;
     8     set<int> coll2;
     9     INSERT_ELEMENTS(coll1,1,6);
    10     INSERT_ELEMENTS(coll2,3,8);
    11     PRINT_ELEMENTS(coll1,"coll1: ");
    12     PRINT_ELEMENTS(coll2,"coll2: ");
    13     cout<<"merged: ";
    14     merge(coll1.begin(),coll1.end(),coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
    15     cout<<endl;
    16 }
    View Code

    2.两个已序集合的并集

    OutputIterator

    set_union(InputIterator source1Beg,InputIterator source1End,

                    InputIterator source2Beg,InputIterator source2End,

                    OutputIterator destBeg)

    OutputIterator

    set_union(InputIterator source1Beg,InputIterator source1End,

                     InputIterator source2Beg,InputIterator source2End,

                     OutputIterator destBeg,BinaryPredicate op)

    与merge不一样的是,目标区间的元素要不来自第一源区间,要不就来自第二源区间,或是同时来自两个源区间。例如:

    source1:1 2 2 4 6 7 7 9

    source2:2 2 2 3 6 6 8 9

    dest:1 2 2 2 3 4 6 6 7 7 8 9 

    3.两个已序集合的交集

    OutputIterator

    set_intersection(InputIterator source1Beg,InputIterator source1End,

                              InputIterator source2Beg,InputIterator source2End,

                              OutputIterator destBeg)

    OutputIterator

    set_intersection(InputIterator source1Beg,InputIterator source1End,

                              InputIterator source2Beg,InputIterator source2End,

                              OutputIterator destBeg,BinaryPredicate op)

    4.两个已序集合的差集

    OutputIterator

    set_difference(InputIterator source1Beg,InputIterator source1End,

                              InputIterator source2Beg,InputIterator source2End,

                              OutputIterator destBeg)

    OutputIterator

    set_difference(InputIterator source1Beg,InputIterator source1End,

                              InputIterator source2Beg,InputIterator source2End,

                              OutputIterator destBeg,BinaryPredicate op)

    目标区间的元素只存在于第一源区间,不存在与第二源区间。

  • 相关阅读:
    vue input输入框验证10的n次方
    Django2.2使用mysql数据库pymysql版本不匹配问题的解决过程与总结
    数据结构(四):树
    数据结构(三):队列
    数据结构(二):栈
    数据结构(一):链表
    python深拷贝与浅拷贝
    python yield、yield from与协程
    python __getattribute__、__getattr__、__setattr__详解
    python迭代、可迭代对象、迭代器及生成器
  • 原文地址:https://www.cnblogs.com/runnyu/p/4849639.html
Copyright © 2011-2022 走看看