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

            eXile 的專欄

            vector的有序化操作

              在有些情況下,需要用到一個有序的vector。它的有序操作有三種:查找,插入,刪除。
              
              插入實現:
            template <typename Container>
            inline void ordered_insert(Container
            & c,  typename Container::value_type const& t)
            {
                c.insert(std::upper_bound(c.begin(), c.end(), t), t);
            }

            template 
            <typename Container, typename Cmp>
            inline void ordered_insert(Container
            & c, typename Container::value_type const& t, Cmp cmp)
            {
                c.insert(std::upper_bound(c.begin(), c.end(), t, cmp), t);
            }
              
              刪除實現:
            template <typename Container, typename It>
            inline void erase_range(Container
            & c, std::pair<It, It> const& r)
            {
                c.erase(r.first, r.second);
            }

            template 
            <typename Container>
            inline void ordered_erase(Container
            & c,  typename Container::value_type const& t)
            {
                erase_range(c, std::equal_range(c.begin(), c.end(), t));
            }

            template 
            <typename Container, typename T, typename Cmp>
            inline void ordered_erase(Container
            & c, T const& t, Cmp cmp)
            {
                erase_range(c, std::equal_range(c.begin(), c.end(), t, cmp));
            }

              查找可通過binary_search, lower_bound, upper_bound, 或者equal_range實現。如果要實現類似map的關鍵字搜索,有一個技巧,就是用比較函數進行重載,比如學生要按學號查找,則用以下定義:
            struct Student
            {
                
            int            id;
                std::
            string name;

                struct LessThan
                {
                    bool operator() (Student 
            const& x, Student const& y)
                    {
                        return x.id 
            < y.id;
                    }

                    bool operator() (Student 
            const& x, int id)
                    {
                        return x.id 
            < id;
                    }

                    bool operator() (
            int id, Student const& y)
                    {
                        return id 
            < y.id;
                    }
                };
            };

            查找學號為5的學生:
            std::vector<Student> students;

            bool exist = std::binary_search(students.begin(), students.end(), 5, Student::LessThan());

            刪除學號為5的學生:
            ordered_erase(students, 5, Student::LessThan());

            posted on 2008-01-29 13:13 eXile 閱讀(4056) 評論(1)  編輯 收藏 引用 所屬分類: C/C++代碼片段STL/BOOST

            評論

            # re: vector的有序化操作 2008-01-30 09:08 FongLuo

            好文,好文~~  回復  更多評論   

            導航

            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            統計

            常用鏈接

            留言簿(18)

            隨筆分類

            隨筆檔案

            服務器編程

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            99久久精品免费观看国产| 亚洲七七久久精品中文国产| 久久久久久人妻无码| 久久精品亚洲日本波多野结衣| 97精品国产91久久久久久| 久久久精品国产Sm最大网站| 久久久久青草线蕉综合超碰| WWW婷婷AV久久久影片| 久久精品国产亚洲Aⅴ香蕉 | 久久人妻少妇嫩草AV无码专区| 国产精品久久久久久久久免费| 久久国产免费直播| 久久国产高潮流白浆免费观看| 久久精品无码一区二区app| 久久久久精品国产亚洲AV无码| 久久精品国产亚洲欧美| 最新久久免费视频| 国产2021久久精品| 久久综合香蕉国产蜜臀AV| 狠狠久久综合伊人不卡| 99久久久精品| 99久久99这里只有免费的精品| 亚洲精品成人网久久久久久| 国产福利电影一区二区三区久久久久成人精品综合 | 亚洲国产成人久久综合碰| 久久线看观看精品香蕉国产| 亚洲国产精品高清久久久| 一级女性全黄久久生活片免费| 91秦先生久久久久久久| 99久久人妻无码精品系列蜜桃| 亚洲国产精品一区二区久久hs| 欧美成a人片免费看久久| 成人a毛片久久免费播放| 欧美精品一区二区精品久久| AAA级久久久精品无码片| 久久久久久久人妻无码中文字幕爆 | 亚洲AV无码久久| 久久无码中文字幕东京热| 欧美亚洲国产精品久久| 久久精品国产亚洲αv忘忧草 | 偷偷做久久久久网站|