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

            woaidongmao

            文章均收錄自他人博客,但不喜標題前加-[轉貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評論 - 661, 引用 - 0
            數據加載中……

            STL stable_sort 穩定排序

            所謂穩定排序,是指對一個序列進行排序之后,如果兩個元素的值相等,則原來亂序時在前面的元素現在(排好序之后)仍然排在前面。STL中提供stable_sort()函數來讓我們進行穩定排序。為了更好的說明穩定排序的效果,我們定義了一個結構體元素,一個value成員和一個index成員,前者表示元素的值,后者表示亂序時的索引。

            stable_sort()內部由歸并排序來實現。

            //Coded by 代碼瘋子

            //http://www.programlife.net/

            #include <iostream>

            #include <vector>

            #include <algorithm>

            #include <iterator>

            using namespace std;

             

            typedef struct TagNode

            {

                    int value;

                    int index;

            }Node;

             

            bool myCmp(const Node& a, const Node& b)

            {

                    return a.value < b.value;

            }

             

            int main(int argc, char **argv)

            {

                    vector<Node> coll;

                    Node tmp;

                    int idx = 0, num;

             

                    while(cin >> num && num)

                    {

                           ++idx;

                           tmp.value = num;

                           tmp.index = idx;

                           coll.push_back(tmp);

                    }

             

                    stable_sort(coll.begin(), coll.end(), myCmp);

             

                    cout << "Index\tValue:" << endl;

                    vector<Node>::iterator pos;

                    for(pos = coll.begin(); pos != coll.end(); ++pos)

                    {

                           cout << pos->index << "\t" << pos->value << endl;

                    }

             

                    return 0;

            }

            程序的運行結果如下圖所示,可以看到,對于元素值相同的元素,索引小的在前面,穩定排序就是這么一個效果。

            clip_image001

             

            posted on 2011-02-17 10:39 肥仔 閱讀(5361) 評論(1)  編輯 收藏 引用 所屬分類: Boost & STL

            評論

            # re: STL stable_sort 穩定排序  回復  更多評論   

            哦哦,說得很好,^_^
            2011-07-23 00:49 | xingyezhi
            国产精品久久久久无码av| 精品久久无码中文字幕| 99久久久精品免费观看国产| 久久精品国产99国产精品导航 | 热久久国产精品| 亚洲国产成人久久一区久久| 久久WWW免费人成—看片| 99久久99久久精品国产| 久久久久久噜噜精品免费直播| 久久99精品久久久久久齐齐| 亚洲国产精品无码久久久不卡 | 99久久99久久精品国产| 色妞色综合久久夜夜| 国产亚洲欧美精品久久久| 超级碰久久免费公开视频| 久久久亚洲裙底偷窥综合| 国产精品久久久久久福利漫画| 国产精品99久久久精品无码| 久久人妻无码中文字幕| 国产AV影片久久久久久| 久久不见久久见免费视频7| 久久综合香蕉国产蜜臀AV| 狠狠久久综合伊人不卡| 99久久国产免费福利| 久久国产免费直播| 久久久久成人精品无码 | 狠狠狠色丁香婷婷综合久久五月| 波多野结衣AV无码久久一区| 久久成人小视频| 亚洲中文字幕无码一久久区| 久久精品亚洲精品国产色婷| 91精品国产综合久久婷婷| 国产日韩久久久精品影院首页| 色综合色天天久久婷婷基地| 久久综合久久综合久久| 久久精品亚洲AV久久久无码| 国产激情久久久久影院| AV色综合久久天堂AV色综合在| 人妻精品久久久久中文字幕69 | 久久久久无码中| 久久精品国产69国产精品亚洲|