• <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 閱讀(4045) 評論(1)  編輯 收藏 引用 所屬分類: C/C++ 、代碼片段 、STL/BOOST

            評論

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

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

            導航

            <2008年1月>
            303112345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            統計

            常用鏈接

            留言簿(18)

            隨筆分類

            隨筆檔案

            服務器編程

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            香蕉aa三级久久毛片 | 精品久久8x国产免费观看| 婷婷伊人久久大香线蕉AV| 狠狠久久亚洲欧美专区| 久久99热这里只频精品6| AV狠狠色丁香婷婷综合久久 | 狠狠色丁香久久婷婷综合| 国产精品久久国产精品99盘 | 99久久国产亚洲综合精品| 国产成人精品久久一区二区三区 | 精品久久久久久亚洲精品| 亚洲精品乱码久久久久久蜜桃 | 久久久久国产精品嫩草影院| 色88久久久久高潮综合影院| 久久久久人妻一区精品| 伊人久久综在合线亚洲2019| 亚洲国产精品18久久久久久| 久久夜色精品国产噜噜亚洲a| 欧美亚洲另类久久综合| 国产精品9999久久久久| 热re99久久精品国99热| 久久久久亚洲av综合波多野结衣| 久久国产精品偷99| 99久久国产综合精品五月天喷水| 精品久久久无码人妻中文字幕豆芽| 伊人久久大香线焦AV综合影院| 手机看片久久高清国产日韩 | 精品久久久噜噜噜久久久| 色8久久人人97超碰香蕉987| 久久久久久久97| 亚洲va久久久噜噜噜久久天堂| 久久精品成人免费看| 精品久久久久久久| 91精品无码久久久久久五月天| 99国内精品久久久久久久| 国产AV影片久久久久久| 久久国产香蕉一区精品| 色诱久久av| 婷婷久久香蕉五月综合加勒比| 久久综合亚洲色一区二区三区| 蜜臀av性久久久久蜜臀aⅴ|