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

            我的玻璃盒子

            (轉載)vector的有序化操作

            原文鏈接:http://m.shnenglu.com/eXile/archive/2008/01/29/42104.html

             

              在有些情況下,需要用到一個有序的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 16:15 深藍色系統 閱讀(409) 評論(0)  編輯 收藏 引用 所屬分類: Skills

            導航

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

            統計

            常用鏈接

            留言簿(75)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲午夜久久久影院伊人| 久久精品国产99久久久古代| 亚洲国产精品久久| 人妻少妇精品久久| 99久久精品免费看国产一区二区三区 | 国产午夜精品久久久久免费视| 99久久精品国内| 欧美精品九九99久久在观看| 国产亚洲婷婷香蕉久久精品| 香蕉久久永久视频| 久久精品国产99国产精偷| 久久婷婷是五月综合色狠狠| 久久精品国产精品亚洲精品| 一本色道久久99一综合| 国内精品久久久久久久涩爱| 无遮挡粉嫩小泬久久久久久久| 久久精品99无色码中文字幕| 一本一本久久A久久综合精品 | 伊人久久无码中文字幕| 精品久久久久久久中文字幕| 久久精品天天中文字幕人妻| 亚洲精品无码久久不卡| 伊人久久综在合线亚洲2019| 久久久精品2019免费观看| 久久综合鬼色88久久精品综合自在自线噜噜| 无码人妻久久一区二区三区 | 人妻无码中文久久久久专区| 亚洲欧美日韩精品久久亚洲区| 曰曰摸天天摸人人看久久久| 天堂久久天堂AV色综合| 久久人与动人物a级毛片| 精品久久久无码中文字幕天天| 精品一区二区久久久久久久网站| 中文字幕无码久久人妻| 久久夜色精品国产亚洲av| 久久久久国色AV免费看图片| 国产成人久久精品麻豆一区| 久久亚洲国产精品一区二区| 亚洲成色999久久网站| 久久噜噜电影你懂的| 国产成人香蕉久久久久|