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

            huyutian

            他強由他強,清風拂山崗;他橫由他橫,明月照大江。他自狠來他自惡,我自一口真氣足

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              20 隨筆 :: 47 文章 :: 22 評論 :: 0 Trackbacks

             

            /*
            set/multiset會根據待定的排序準則,自動將元素排序。兩者不同在于前者不允許元素重復,而后者允許。
            1) 不能直接改變元素值,因為那樣會打亂原本正確的順序,要改變元素值必須先刪除舊元素,則插入新元素
            2) 不提供直接存取元素的任何操作函數,只能通過迭代器進行間接存取,而且從迭代器角度來看,元素值是常數
            3) 元素比較動作只能用于型別相同的容器(即元素和排序準則必須相同)
            set模板原型://Key為元素(鍵值)類型
            template <class Key, class Compare=less<Key>, class Alloc=STL_DEFAULT_ALLOCATOR(Key) >
            從原型可以看出,可以看出比較函數對象及內存分配器采用的是默認參數,因此如果未指定,它們將采用系統(tǒng)默認方式,
            另外,利用原型,可以有效地輔助分析創(chuàng)建對象的幾種方式
            */

            #include 
            <iostream>
            #include 
            <string>
            #include 
            <set>

            using namespace std;

            struct strLess
            {
               
            bool operator() (const char *s1, const char *s2) const
               
            {
                
            return strcmp(s1, s2) < 0;
               }

            }
            ;

            void printSet(set<int> s)
            {
            copy(s.begin(), s.end(), ostream_iterator
            <int>(cout, "") );

            // set<int>::iterator iter;
            // for (iter = s.begin(); iter != s.end(); iter++)
            //    //cout<<"set["<<iter-s.begin()<<"]="<<*iter<<", "; //Error
            //    cout<<*iter<<", ";
            cout<<endl;
            }


            void main()
            {
            //創(chuàng)建set對象,共5種方式,提示如果比較函數對象及內存分配器未出現,即表示采用的是系統(tǒng)默認方式
            //創(chuàng)建空的set對象,元素類型為int,
            set<int> s1; 
            //創(chuàng)建空的set對象,元素類型char*,比較函數對象(即排序準則)為自定義strLess
            set<const char*, strLess> s2( strLess); 
            //利用set對象s1,拷貝生成set對象s2
            set<int> s3(s1); 
            //用迭代區(qū)間[&first, &last)所指的元素,創(chuàng)建一個set對象
            int iArray[] = {133219};
            set<int> s4(iArray, iArray + 3);
            //用迭代區(qū)間[&first, &last)所指的元素,及比較函數對象strLess,創(chuàng)建一個set對象
            const char* szArray[] = {"hello""dog""bird" };
            set<const char*, strLess> s5(szArray, szArray + 3, strLess() );

            //元素插入:
            //1,插入value,返回pair配對對象,可以根據.second判斷是否插入成功。(提示:value不能與set容器內元素重復)
            //pair<iterator, bool> insert(value)
            //2,在pos位置之前插入value,返回新元素位置,但不一定能插入成功
            //iterator insert(&pos, value)
            //3,將迭代區(qū)間[&first, &last)內所有的元素,插入到set容器
            //void insert[&first, &last)
            cout<<"s1.insert() : "<<endl;
            for (int i = 0; i <5 ; i++)
                s1.insert(i
            *10);
            printSet(s1);

            cout
            <<"s1.insert(20).second = "<<endl;;
            if (s1.insert(20).second)
                cout
            <<"Insert OK!"<<endl;
            else
                cout
            <<"Insert Failed!"<<endl;

            cout
            <<"s1.insert(50).second = "<<endl;
            if (s1.insert(50).second)
            {cout<<"Insert OK!"<<endl; printSet(s1);}
            else
                cout
            <<"Insert Failed!"<<endl;

            cout
            <<"pair<set<int>::iterator::iterator, bool> p;\np = s1.insert(60);\nif (p.second):"<<endl;
            pair
            <set<int>::iterator::iterator, bool> p;
            = s1.insert(60);
            if (p.second)
            {cout<<"Insert OK!"<<endl; printSet(s1);}
            else
               cout
            <<"Insert Failed!"<<endl;

            //元素刪除
            //1,size_type erase(value) 移除set容器內元素值為value的所有元素,返回移除的元素個數
            //2,void erase(&pos) 移除pos位置上的元素,無返回值
            //3,void erase(&first, &last) 移除迭代區(qū)間[&first, &last)內的元素,無返回值
            //4,void clear(), 移除set容器內所有元素

            cout
            <<"\ns1.erase(70) = "<<endl;
            s1.erase(
            70);
            printSet(s1);
            cout
            <<"s1.erase(60) = "<<endl;
            s1.erase(
            60);
            printSet(s1);

            cout
            <<"set<int>::iterator iter = s1.begin();\ns1.erase(iter) = "<<endl;
            set<int>::iterator iter = s1.begin();
            s1.erase(iter);
            printSet(s1);

            //元素查找
            //count(value)返回set對象內元素值為value的元素個數
            //iterator find(value)返回value所在位置,找不到value將返回end()
            //lower_bound(value),upper_bound(value), equal_range(value) 略
            cout<<"\ns1.count(10) = "<<s1.count(10)<<", s1.count(80) = "<<s1.count(80)<<endl;
            cout
            <<"s1.find(10) : ";
            if (s1.find(10!= s1.end()) 
                cout
            <<"OK!"<<endl;
            else
                cout
            <<"not found!"<<endl;

            cout
            <<"s1.find(80) : ";
            if (s1.find(80!= s1.end()) 
                cout
            <<"OK!"<<endl;
            else
                cout
            <<"not found!"<<endl;

            //其它常用函數
            cout<<"\ns1.empty()="<<s1.empty()<<", s1.size()="<<s1.size()<<endl;
            set<int> s9;
            s9.insert(
            100);
            cout
            <<"s1.swap(s9) :"<<endl;
            s1.swap(s9);
            cout
            <<"s1: "<<endl;
            printSet(s1);
            cout
            <<"s9: "<<endl;
            printSet(s9);
            //lower_bound,upper_bound,equal_range(略)
            }



            ///////////////i測試結果/////////////////////////
            s1.insert() :
            010203040,
            s1.insert(
            20).second =
            Insert Failed
            !
            s1.insert(
            50).second =
            Insert OK
            !
            01020304050,
            pair
            <set<int>::iterator::iterator, bool> p;
            = s1.insert(60);
            if (p.second):
            Insert OK
            !
            0102030405060,

            s1.erase(
            70=
            0102030405060,
            s1.erase(
            60=
            01020304050,
            set<int>::iterator iter = s1.begin();
            s1.erase(iter) 
            =
            1020304050,

            s1.count(
            10= 1, s1.count(80= 0
            s1.find(
            10) : OK!
            s1.find(
            80) : not found!

            s1.empty()
            =0, s1.size()=5
            s1.swap(s9) :
            s1:
            100,
            s9:
            1020304050,
            posted on 2010-02-07 23:55 胡雨田 閱讀(52000) 評論(0)  編輯 收藏 引用 所屬分類: 編程技巧
            蜜臀av性久久久久蜜臀aⅴ| 东方aⅴ免费观看久久av| 国内精品九九久久久精品| 青青青伊人色综合久久| 国产午夜精品久久久久九九| 94久久国产乱子伦精品免费| 色悠久久久久久久综合网| 精品久久8x国产免费观看| 91久久精品国产91性色也| 亚洲天堂久久久| 99久久er这里只有精品18| 色婷婷狠狠久久综合五月| 久久精品无码专区免费青青| 性高朝久久久久久久久久| 久久精品国产亚洲综合色| 国内精品伊人久久久久777| 精品99久久aaa一级毛片| 97精品伊人久久大香线蕉app| 久久国产精品无| 亚洲精品乱码久久久久久蜜桃不卡 | 亚洲国产成人久久精品99| 国内精品久久久久影院免费 | 亚洲国产精品无码久久一线| 7国产欧美日韩综合天堂中文久久久久| 久久91精品国产91久| 狠狠色伊人久久精品综合网 | 思思久久99热免费精品6| 久久免费精品视频| 狠狠色丁香婷综合久久| 无码人妻精品一区二区三区久久 | 久久精品一区二区三区中文字幕| 亚洲人成电影网站久久| 久久精品成人免费看| 久久99久久99精品免视看动漫| 精品久久久久久久国产潘金莲 | 色综合久久久久综合体桃花网| 亚洲精品WWW久久久久久| 国产精品嫩草影院久久| 国内精品久久久久久久影视麻豆 | 久久91精品国产91| 亚洲精品无码久久久久sm|