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

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

            導航

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

            統計

            常用鏈接

            留言簿(75)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            99国内精品久久久久久久| 国产69精品久久久久9999| 欧美黑人激情性久久| 亚洲色欲久久久久综合网| 99久久国产亚洲综合精品| 亚洲精品乱码久久久久久| 国产成人久久精品激情| 亚洲一区中文字幕久久| 久久综合久久鬼色| 亚洲精品乱码久久久久久蜜桃不卡 | 久久亚洲精品无码观看不卡| 久久国产成人午夜aⅴ影院 | 91久久精品91久久性色| 久久精品9988| 一本久久a久久精品综合香蕉| 亚洲中文字幕无码久久2017| 国产精品久久久久久搜索 | 欧美粉嫩小泬久久久久久久 | 天堂无码久久综合东京热| 一级做a爰片久久毛片毛片| 久久久免费精品re6| 精品久久久久久无码免费| 成人久久免费网站| 久久久久国产一级毛片高清板| 日本欧美久久久久免费播放网| 一级做a爰片久久毛片16| 亚洲愉拍99热成人精品热久久| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久se精品一区二区| 久久久久人妻一区二区三区| 青青青国产精品国产精品久久久久| 欧美日韩久久中文字幕| 亚洲国产精品人久久| 日本久久久久亚洲中字幕| 伊人久久一区二区三区无码| 久久久人妻精品无码一区| 97精品伊人久久久大香线蕉 | 亚洲嫩草影院久久精品| 欧洲人妻丰满av无码久久不卡| 狠狠综合久久AV一区二区三区| 2020久久精品亚洲热综合一本|