• <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 哲學與程序 閱讀(6643) 評論(0)  編輯 收藏 引用 所屬分類: AlgorithmC & C++C++ STL

            導航

            公告

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

            常用鏈接

            隨筆分類(37)

            隨筆檔案(41)

            Algorithm

            最新隨筆

            搜索

            最新評論

            獨立博客: 哲學與程序
            日本精品久久久久中文字幕| 亚洲欧美国产日韩综合久久| 久久精品国产久精国产思思| 久久国产欧美日韩精品| 久久成人影院精品777| 久久97久久97精品免视看| 狠狠色婷婷久久一区二区| 久久精品国产清高在天天线| 久久久WWW免费人成精品| 亚洲AV无码久久精品色欲| 99久久免费国产精品| 亚洲精品无码成人片久久| 国产一区二区精品久久岳| 97久久婷婷五月综合色d啪蜜芽| 国产精品久久永久免费| 亚洲女久久久噜噜噜熟女| 久久精品国产亚洲一区二区三区| 久久久婷婷五月亚洲97号色| 四虎久久影院| 久久人搡人人玩人妻精品首页| 成人综合伊人五月婷久久| 亚洲天堂久久久| 欧美粉嫩小泬久久久久久久| AA级片免费看视频久久| 国内精品久久久久久久97牛牛| 一本久久精品一区二区| 99久久国产宗和精品1上映| 97香蕉久久夜色精品国产| 国产精品狼人久久久久影院| 国产精品久久久久久| 97久久超碰国产精品旧版| 中文字幕无码久久久| 久久精品国产99国产精品| 国产精品欧美久久久久天天影视| 久久国产精品99精品国产| 久久婷婷五月综合色高清| 亚洲午夜久久久影院| 久久亚洲国产精品成人AV秋霞| 亚洲婷婷国产精品电影人久久| 四虎亚洲国产成人久久精品| 无码人妻少妇久久中文字幕 |