<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 30,  comments - 28,  trackbacks - 0

    ???? 很經典的"轉圈數數踢人"問題,老早以前做的東西
    ???? 這里是我的解法,利用STL里的List構建Ring List
    ???? 頭文件:
    ? //:This part is partly from Bruce Eckel's source code .
    // thought he don't hear of me at all ^_^
    // Making a "ring" data structure from the STL
    #ifndef RING_H
    #define RING_H

    #include<iterator>
    #include<list>
    using namespace std;

    template <class Type> class Ring{
    ?list<Type> lst;
    public:


    ?class iterator;
    ?friend class iterator;
    ?class iterator :public std::iterator<std::bidirectional_iterator_tag,Type,ptrdiff_t>{
    ??typename list<Type>::iterator it;
    ??list<Type>* r;

    ?public:
    ??iterator(list<Type>&lst,const typename list<Type>::iterator& i):it(i),r(&lst){}

    ??bool operator==(const iterator& x)const{
    ???return it==x.it;
    ??}

    ??bool operator!=(const iterator& x)const{
    ???return!(*this==x);
    ??}

    ??typename list<Type>::reference operator*()const{
    ???return *it;
    ??}

    ??iterator& operator++(){
    ???++it;
    ???if(it==r->end())
    ????it=r->begin();
    ???return *this;
    ??}

    ??iterator operator++(int){
    ???iterator tmp=*this;
    ???++*this;
    ???return tmp;
    ??}

    ??iterator &operator--(){
    ???if(it==r->begin())
    ????it=r->end();
    ???--it;
    ???return *this;
    ??}
    ??iterator operator--(int){
    ???iterator tmp=*this;
    ???--*this;
    ???return tmp;
    ??}

    ??iterator insert(const Type& x){
    ???return iterator(*r,r->insert(it,x));
    ??}
    ??
    ??//I have to recognize that the iterator is not so smart as
    ??//we expected .If the element in the list is erased ,the
    ??//iterator will be lost and point to a null.The shortage of
    ??//the stupid iterator will been seen the the Josephus.cpp
    ??//we have to use a template variable to storage the next iterator
    ??//of the deleting iterator.If not ,the programe will be nightmare
    ??iterator erase(){
    ???return iterator(*r,r->erase(it));
    ??}

    ?};

    ?void push_back(const Type& x){lst.push_back(x);}

    ?iterator begin(){return iterator(lst,lst.begin());}

    ?int size(){return lst.size();}

    };
    #endif

    實現文件:
    //:Using circle list to solve Josephus problem
    //:version 1.01
    //:author Murainwood 12/3/2006
    #include<iostream>
    #include"Ring.h"

    void? main(){

    ?//enter the number of people and the index

    ?int n,m;
    ?cout<<"Enter the Number of Contestants?";
    ?cin>>n>>m;
    ???
    ?
    ??? Ring<int> ring;
    ?for(int i=1;i<=n;i++) ring.push_back(i);

    ?//determine the iterator
    ?//it is reasonable? to declare two iterator

    ??? Ring<int>::iterator tmp=ring.begin();
    ?Ring<int>::iterator it=tmp;
    ?
    ?//without the iterator tmp ,if we erase the it
    ?//the it will lost ,so the loop can not? work

    ?for(int index=0;index<n-1;index++){
    ???it=tmp;
    ??for(int j=0;j<m-1;j++){it++;tmp++;}
    ???tmp++;
    ????? cout<<"Delete person"<<*it<<endl;
    ???it.erase();
    ???
    ?}
    ?it=ring.begin();
    ?cout<<"The result is person"<<*it<<endl;
    }

    //:finished

    ???? 這個Ring List有很高的效率和安全性,并且通用性也比較好
    ???? 這里也發現個問題,就是STL里的Iterator 還不夠"聰明",這也是為什么我會在程序里多加個迭代器:tmp,
    因為如果我調用了it.erase();那么這個迭代器就丟失了,程序也就無非運行下去.
    ???? 這樣我們很容易地想,如果調用了it.erase()后,指針不是丟失,而是指向下一個元素該多好啊!
    ???? 很遺憾,現階段標準STL庫里的List沒有這個功能.猜想是因為List的物理存儲結構的緣故,如果讓他的迭代器變得如我們想像得那么"聰明",那么花費可能會有些大.
    ??? 還沒有驗證過boost庫,不知道那里的迭代器是否會更人性化些
    ???? 有時間研究一下相關的STL源碼,寫一個"聰明些"的迭代器.

    posted on 2006-04-18 00:10 murainwood 閱讀(651) 評論(0)  編輯  收藏 所屬分類: C++&C

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2006年4月>
    2627282930311
    2345678
    9101112131415
    16171819202122
    23242526272829
    30123456

    如果真的給你一片天,你敢不敢要?

    常用鏈接

    留言簿(3)

    隨筆分類

    隨筆檔案

    相冊

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久久久亚洲av无码专区蜜芽| 四虎影视免费永久在线观看| 中文字幕人成人乱码亚洲电影| 亚洲AV无码AV男人的天堂不卡| 好男人www免费高清视频在线| 亚洲色图视频在线观看| 鲁大师在线影院免费观看| 亚洲一区二区在线免费观看| 国产精品免费看久久久| 亚洲视频一区调教| 99久久人妻精品免费二区| 亚洲精品成人网站在线播放| 日本免费网址大全在线观看| 亚洲男人天堂2018av| 扒开双腿猛进入爽爽免费视频| 亚洲国产AV无码一区二区三区 | 亚洲乱码无限2021芒果| 国产成人精品免费视频大全麻豆| 亚洲一级片在线观看| 成年性羞羞视频免费观看无限| 亚洲AⅤ男人的天堂在线观看| 免费亚洲视频在线观看| A毛片毛片看免费| 亚洲一区二区三区四区在线观看| 97碰公开在线观看免费视频| 亚洲日本在线电影| 亚洲精品麻豆av| 特级精品毛片免费观看| 亚洲av无码片在线观看| 免费观看四虎精品国产永久| 中国性猛交xxxxx免费看| 亚洲自偷精品视频自拍| 蜜桃精品免费久久久久影院| www在线观看播放免费视频日本| 久久噜噜噜久久亚洲va久| 最近免费中文字幕视频高清在线看| 直接进入免费看黄的网站| 国产亚洲一区二区精品| 久久精品免费一区二区喷潮 | 99精品在线免费观看| 亚洲另类无码一区二区三区|