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

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

            導航

            <2007年10月>
            30123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            統計

            常用鏈接

            留言簿(18)

            隨筆分類

            隨筆檔案

            服務器編程

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久综合九色综合97_久久久| 一本综合久久国产二区| 国内精品久久九九国产精品| 青青草国产精品久久久久| 欧美午夜A∨大片久久| 久久亚洲熟女cc98cm| 中文字幕久久欲求不满| 中文字幕精品久久| 国产毛片久久久久久国产毛片 | 色狠狠久久综合网| 久久香蕉综合色一综合色88| 久久久久久久久66精品片| 精品久久久久久久| 亚洲国产精品无码久久久蜜芽| 国产精品久久久久一区二区三区| 亚洲国产精品久久电影欧美| 精品视频久久久久| 亚洲国产精品久久久久婷婷软件| 久久精品免费一区二区| 久久综合久久伊人| 久久伊人五月天论坛| 精品久久久久久久久久中文字幕| 久久久免费精品re6| 日产精品久久久一区二区| 久久综合九色综合网站| 亚洲?V乱码久久精品蜜桃| 国产成人久久精品麻豆一区| 青青草原综合久久| AAA级久久久精品无码片| 无码日韩人妻精品久久蜜桃| 精品久久久久久国产| 三级片免费观看久久| 欧美一级久久久久久久大片| 亚洲国产成人精品91久久久 | 国产成人久久精品一区二区三区| 久久无码人妻精品一区二区三区 | 一极黄色视频久久网站| 91麻豆国产精品91久久久| 久久这里只精品99re66| 亚洲av成人无码久久精品| 人妻丰满AV无码久久不卡|