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

            最新隨筆

            搜索

            最新評論

            獨立博客: 哲學與程序
            久久久久成人精品无码| 国产日韩久久久精品影院首页| 香蕉久久夜色精品国产尤物| 久久精品国产亚洲AV蜜臀色欲| 精品久久久噜噜噜久久久 | 亚洲国产精品成人久久| 久久精品国产一区| 久久久久久免费视频| 亚洲嫩草影院久久精品| 人人狠狠综合88综合久久| 69SEX久久精品国产麻豆| 久久久久国产视频电影| 麻豆一区二区99久久久久| 久久精品亚洲欧美日韩久久| 97久久超碰成人精品网站| 天天综合久久一二三区| 99久久免费国产特黄| 伊人久久综合成人网| 久久久久久午夜精品| 久久国产成人午夜AV影院| 久久精品国产亚洲麻豆| 久久99国内精品自在现线| 囯产极品美女高潮无套久久久| 欧美亚洲另类久久综合婷婷| 99热热久久这里只有精品68| 人妻无码久久一区二区三区免费| 少妇久久久久久被弄到高潮| 久久久91人妻无码精品蜜桃HD | 久久国内免费视频| 亚洲国产成人精品91久久久 | 91精品国产91久久久久福利| 久久久久久久97| 18禁黄久久久AAA片| 人人妻久久人人澡人人爽人人精品 | 国产精品久久久久aaaa| 久久99精品久久久久久久不卡 | WWW婷婷AV久久久影片| 青春久久| 久久人人爽爽爽人久久久| 国产精品视频久久久| 狠狠色噜噜色狠狠狠综合久久 |