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

            最新隨筆

            搜索

            最新評論

            獨立博客: 哲學與程序
            久久久精品人妻一区二区三区蜜桃 | 久久夜色精品国产噜噜麻豆| 久久中文字幕精品| 精品国产青草久久久久福利| 国产精品9999久久久久| 久久91精品久久91综合| 久久91精品综合国产首页| 久久精品免费全国观看国产| 狠狠狠色丁香婷婷综合久久五月| 精品欧美一区二区三区久久久 | 伊人久久国产免费观看视频| 伊人久久大香线蕉av不卡| 91久久精品国产91性色也| 99久久综合国产精品免费| 久久成人精品| 久久中文骚妇内射| 久久久久久国产精品无码下载| 国产精品久久久久…| 亚洲中文久久精品无码ww16| 狠狠色丁香婷婷综合久久来来去| 欧美黑人又粗又大久久久| 久久精品国产72国产精福利| 久久精品国产亚洲AV无码麻豆| 亚洲?V乱码久久精品蜜桃| 欧美亚洲另类久久综合| 欧洲成人午夜精品无码区久久| 午夜精品久久久内射近拍高清| 99久久人人爽亚洲精品美女 | 久久亚洲2019中文字幕| 99久久精品影院老鸭窝| 久久精品aⅴ无码中文字字幕重口 久久精品a亚洲国产v高清不卡 | 少妇高潮惨叫久久久久久 | 色偷偷88888欧美精品久久久| 久久精品国产72国产精福利| 国产成人久久久精品二区三区| 国产精品久久久久影院色| 人妻精品久久无码专区精东影业| 欧美亚洲国产精品久久| 久久人人爽人人爽人人片AV高清| 少妇人妻综合久久中文字幕| 精品综合久久久久久97|