• <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://www.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)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            影音先锋女人AV鲁色资源网久久 | 国产香蕉久久精品综合网| 中文字幕乱码人妻无码久久| 久久播电影网| 久久精品无码一区二区日韩AV| 久久精品国产99国产电影网| 国产麻豆精品久久一二三| 久久人人爽人人爽人人片AV不| 无码日韩人妻精品久久蜜桃| 久久免费的精品国产V∧| 久久久亚洲欧洲日产国码aⅴ| 久久99精品久久久久久动态图| 久久精品国产亚洲AV大全| 99久久99这里只有免费的精品| 久久精品国产亚洲沈樵| 精品久久久久久无码免费| 欧美激情精品久久久久久久| 久久久国产精华液| 久久精品国产亚洲AV无码偷窥| 四虎国产精品免费久久久| 国内精品久久久久久久影视麻豆| 久久久久99精品成人片三人毛片| 亚洲精品97久久中文字幕无码| 亚洲综合日韩久久成人AV| 久久91亚洲人成电影网站| 精品久久久久久无码人妻热 | 久久精品无码一区二区无码| 久久最近最新中文字幕大全| 色悠久久久久久久综合网| 久久这里只有精品18| 国产精品成人99久久久久 | 久久精品国产亚洲AV无码麻豆| 狠狠综合久久综合中文88| 久久久久久久波多野结衣高潮| 久久夜色精品国产亚洲| 99精品国产免费久久久久久下载| 久久亚洲精品视频| 亚洲av日韩精品久久久久久a| 久久久亚洲精品蜜桃臀| 99久久久国产精品免费无卡顿| 日韩中文久久|