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

            最新隨筆

            搜索

            最新評論

            獨立博客: 哲學與程序
            东京热TOKYO综合久久精品| 亚洲国产精品无码久久98| 久久精品免费观看| 亚洲国产精品婷婷久久| 51久久夜色精品国产| 久久精品无码免费不卡| 久久精品国产乱子伦| 女人香蕉久久**毛片精品| 国产亚洲色婷婷久久99精品91| 亚洲国产精品无码久久久久久曰 | 欧美精品国产综合久久| 亚洲精品无码成人片久久| 国产成人香蕉久久久久| 久久精品一本到99热免费| 国产呻吟久久久久久久92| 亚洲乱码精品久久久久..| 久久精品亚洲福利| 久久91精品国产91久久麻豆| 无码任你躁久久久久久老妇| 久久免费线看线看| 久久精品国产99久久久古代 | 久久综合一区二区无码| 麻豆成人久久精品二区三区免费 | 国内精品久久久久影院免费| 伊人久久无码中文字幕| 欧美精品丝袜久久久中文字幕| 久久国产免费观看精品3| 无码国内精品久久综合88| 精品久久久久国产免费| 亚洲国产成人久久精品动漫| 久久精品99久久香蕉国产色戒| 久久精品人人做人人爽电影| 欧美精品福利视频一区二区三区久久久精品 | 久久国产精品二国产精品| 久久精品国产91久久综合麻豆自制| 国产aⅴ激情无码久久| 色狠狠久久综合网| 久久婷婷五月综合色99啪ak| 久久久久亚洲AV无码专区桃色| 久久久精品久久久久特色影视| 精品一久久香蕉国产线看播放 |