• <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>
            獨立博客: 哲學與程序

            哲學與程序

            STL之Binary search

                STL中對于有序序列(vector,list等)提供了相當相當強大的二分搜索Binary search算法。對于可以隨機訪問容器(如vector等),binary search負載度為對數級別(LogN),對于非隨機訪問容器(如list),則算法復雜度為線性。現在簡要介紹一下幾種常用的binary search算法:

            ForwardIterator lower_bound (ForwardIterator first,ForwardIterator last, const T& value)
            ForwardIterator lower_bound (ForwardIterator first,ForwardIterator last, const T& value, Compare comp)
            //查找最遠的一個iterator i, 使得[first,i)里面的任意一個iterator j,有 *j < value or comp(*j,value)==true.

            ForwardIterator upper_bound (ForwardIterator first,ForwardIterator last, const T& value)
            ForwardIterator upper_bound (ForwardIterator first,ForwardIterator last, const T& value, Compare comp)
            //查找最遠的一個iterator i, 使得[first,i)里面的任意一個iterator j,有 !(*j < value) or comp(*j,value)==false.

            pair equal_range(ForwardIterator first, ForwardIterator last, const T& value)
            pair equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp)
            //查找最大的subrange[i,j),使得任意一個iterator in [i,j)滿足!(*k<value) && !(value<*k) or comp(*k,value)==false && comp(value,*k)==false.

            bool binary_search (ForwardIterator first, ForwardIterator last, const T& value)
            bool binary_search (ForwardIterator first, ForwardIterator last, const T& value, Compare comp)
            // 查找是否在[first,last)中存在iterator i,滿足 !(*i<value) && !(value<*i) or comp(*i,value)==false && comp(value,*i)==false
            // 存在則返回true,否則返回false. 

            #include<algorithm>
            #include
            <iostream>
            #include
            <vector>
            #include
            <iterator>
            using namespace std;
            int main()
            {
                vector
            <int>v;
                vector
            <int>::iterator itr;
                pair
            < vector<int>::iterator, vector<int>::iterator >vecpair;
                
                
            for(int i = 1; i <= 20; i++){
                    v.push_back(i
            %6);
                }
                sort(v.begin(),v.end());
                cout 
            << "array: " << endl << "    ";
                copy(v.begin(),v.end(),ostream_iterator
            <int>(cout," "));
                cout 
            << endl;
                
                
            //lower_bound
                cout << "lower_bound function, value = 3:" << endl; 
                itr 
            = lower_bound(v.begin(),v.end(),3);
                cout 
            << "    [first, itr) = ";
                copy(v.begin(),itr,ostream_iterator
            <int>(cout," "));
                cout 
            << endl;
                cout 
            << "    [itr, last) = ";
                copy(itr,v.end(),ostream_iterator
            <int>(cout," "));
                cout 
            << endl << endl;
                
                
            // upper_bound
                cout << "upper_bound function, value = 3:" << endl; 
                itr 
            = upper_bound(v.begin(),v.end(),3);
                cout 
            << "    [first, itr) = ";
                copy(v.begin(),itr,ostream_iterator
            <int>(cout," "));
                cout 
            << endl;
                cout 
            << "    [itr, last) = ";
                copy(itr,v.end(),ostream_iterator
            <int>(cout," "));
                cout 
            << endl << endl;
                
                
            // equal_range
                cout << "equal_range function, value = 3:" << endl; 
                vecpair 
            = equal_range(v.begin(),v.end(),3);
                
                cout 
            << "    [vecpair->first, vecpair->second) = ";
                copy(vecpair.first, vecpair.second, ostream_iterator
            <int>(cout," "));
                cout 
            << endl << endl;
                
                
            //binary_search, value = 3
                cout << "binary_search function, value = 3:" << endl;
                cout 
            << "3 is " << (binary_search(v.begin(),v.end(),3? "":"not "<< "in array." << endl;
                cout 
            << endl;
                
                
            //binary_search, value = 6
                cout << "binary_search function, value = 6:" << endl;
                cout 
            << "6 is " << (binary_search(v.begin(),v.end(),6? "":"not "<< "in array." << endl;
                cout 
            << endl;
                
                
            return 0;    
            }
             

            array:
                
            0 0 0 1 1 1 1 2 2 2 2 3 3 3 4 4 4 5 5 5
            lower_bound function, value 
            = 3:
                [first, itr) 
            = 0 0 0 1 1 1 1 2 2 2 2
                [itr, last) 
            = 3 3 3 4 4 4 5 5 5

            upper_bound function, value 
            = 3:
                [first, itr) 
            = 0 0 0 1 1 1 1 2 2 2 2 3 3 3
                [itr, last) 
            = 4 4 4 5 5 5

            equal_range function, value 
            = 3:
                [vecpair
            ->first, vecpair->second) = 3 3 3

            binary_search function, value 
            = 3:
            3 is in array.

            binary_search function, value 
            = 6:
            6 is not in array.


            posted on 2011-01-16 22:40 哲學與程序 閱讀(6639) 評論(0)  編輯 收藏 引用 所屬分類: AlgorithmC & C++C++ STL

            導航

            公告

            歡迎訪問 http://zhexue.sinaapp.com

            常用鏈接

            隨筆分類(37)

            隨筆檔案(41)

            Algorithm

            最新隨筆

            搜索

            最新評論

            獨立博客: 哲學與程序
            久久国产精品99精品国产| 久久香综合精品久久伊人| 午夜不卡888久久| 久久精品成人| 激情久久久久久久久久| 国产精品日韩深夜福利久久| 久久久久国产亚洲AV麻豆| 综合久久一区二区三区| 日产精品久久久久久久| 成人妇女免费播放久久久| 一本一本久久a久久精品综合麻豆| 性做久久久久久免费观看| 亚洲精品高清国产一线久久| 精品久久一区二区| 波多野结衣久久一区二区| 久久精品亚洲精品国产色婷| 亚洲国产精品久久久久网站| 欧美亚洲日本久久精品| 精品久久久久久无码专区不卡| 国产精品亚洲综合专区片高清久久久 | 亚洲av伊人久久综合密臀性色| 久久精品中文字幕无码绿巨人| 久久久久亚洲av毛片大| 国产精品无码久久综合网| 亚洲欧美日韩中文久久| 久久久久无码专区亚洲av| 欧美大香线蕉线伊人久久| 亚洲第一永久AV网站久久精品男人的天堂AV | 婷婷综合久久中文字幕蜜桃三电影| 国产99久久久国产精品~~牛| 2021国内精品久久久久久影院| 99久久无码一区人妻| 99国产欧美久久久精品蜜芽| 伊人久久精品无码av一区| 久久亚洲电影| 亚洲欧美国产日韩综合久久 | 国产精自产拍久久久久久蜜| 99久久99这里只有免费费精品| 漂亮人妻被黑人久久精品| 欧美大香线蕉线伊人久久| 亚洲国产精品高清久久久|