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

            評論

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

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

            導航

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

            統計

            常用鏈接

            留言簿(18)

            隨筆分類

            隨筆檔案

            服務器編程

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产三级观看久久| 99久久精品无码一区二区毛片| 99久久精品费精品国产一区二区| 亚洲午夜久久久影院| 996久久国产精品线观看| 国产精品久久久久无码av| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 久久久久女人精品毛片| yellow中文字幕久久网| 亚洲欧美成人久久综合中文网| 久久精品国产亚洲AV高清热| 2021久久精品国产99国产精品| 999久久久国产精品| 免费精品久久久久久中文字幕| 久久久久久免费一区二区三区 | 久久久久久午夜精品| 2021精品国产综合久久| 久久综合亚洲色HEZYO国产| 伊人久久大香线蕉综合5g| 久久无码人妻一区二区三区| 亚洲av伊人久久综合密臀性色| 久久人与动人物a级毛片| 夜夜亚洲天天久久| 亚洲AV无码久久寂寞少妇| 久久九九久精品国产免费直播| 亚洲国产精品无码久久久久久曰| 无码人妻精品一区二区三区久久久| 国产精品成人精品久久久| 日韩人妻无码精品久久久不卡| 人妻无码精品久久亚瑟影视| 欧美色综合久久久久久| 国产精品久久永久免费| 看久久久久久a级毛片| 欧美精品乱码99久久蜜桃| 亚洲欧美伊人久久综合一区二区 | 久久久一本精品99久久精品88| 久久久这里有精品| 久久国产香蕉视频| 久久综合视频网站| 精品免费久久久久国产一区| 亚洲精品久久久www|