• <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。它的有序操作有三種:查找,插入,刪除。
              插入實現(xiàn):

            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);
            }

              刪除實現(xiàn):

            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實現(xiàn)。如果要實現(xiàn)類似map的關鍵字搜索,有一個技巧,就是用比較函數(shù)進行重載,比如學生要按學號查找,則用以下定義:

            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 深藍色系統(tǒng) 閱讀(409) 評論(0)  編輯 收藏 引用 所屬分類: Skills

            導航

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

            統(tǒng)計

            常用鏈接

            留言簿(75)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            一97日本道伊人久久综合影院| 亚洲va久久久久| 噜噜噜色噜噜噜久久| 伊人久久大香线蕉综合5g| 亚洲AV无码一区东京热久久| 国产成人精品久久免费动漫| 欧美成a人片免费看久久| 久久精品亚洲一区二区三区浴池 | 99热精品久久只有精品| 99久久夜色精品国产网站| A级毛片无码久久精品免费| 久久夜色精品国产亚洲| 欧美久久久久久午夜精品| 99久久国产热无码精品免费| 伊人久久亚洲综合影院| 91亚洲国产成人久久精品| 国产精品岛国久久久久| 国产精品久久久久a影院| 国内精品久久久久久久coent | 亚洲国产小视频精品久久久三级 | 亚洲欧美精品一区久久中文字幕| 999久久久无码国产精品| 少妇熟女久久综合网色欲| 国产精品无码久久四虎| 色综合合久久天天综合绕视看| 久久亚洲国产中v天仙www| 伊人久久综合精品无码AV专区| 久久免费国产精品| 久久国产成人午夜aⅴ影院 | 青青草原1769久久免费播放| 久久久久AV综合网成人| 久久亚洲私人国产精品vA| 国产精品毛片久久久久久久| 久久国产欧美日韩精品| 久久久久人妻一区二区三区vr| 久久久久99精品成人片欧美| 久久精品无码专区免费青青| 精品无码久久久久久午夜| 久久99热精品| 一本色综合久久| 亚洲乱码精品久久久久..|