<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 閱讀(650) 評論(0)  編輯  收藏 所屬分類: C++&C

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


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

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

    常用鏈接

    留言簿(3)

    隨筆分類

    隨筆檔案

    相冊

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲综合色一区二区三区| 亚洲最新在线视频| 免费一级特黄特色大片| 香蕉高清免费永久在线视频| 亚洲免费电影网站| 无码一区二区三区免费视频| 久久久久se色偷偷亚洲精品av | 亚洲男人的天堂网站| 免费无码肉片在线观看| 亚洲欧美国产欧美色欲| 热99re久久免费视精品频软件| 在线亚洲精品视频| 亚洲裸男gv网站| 国产午夜成人免费看片无遮挡 | 久久电影网午夜鲁丝片免费| 成人亚洲国产va天堂| 国产免费资源高清小视频在线观看| 国产精品亚洲а∨无码播放不卡| 亚洲熟妇少妇任你躁在线观看无码| 一级毛片试看60分钟免费播放| 日本红怡院亚洲红怡院最新| 最近2019中文字幕免费大全5| 456亚洲人成影院在线观| 国产青草视频免费观看97| 一个人看的在线免费视频| 亚洲AV无码精品色午夜果冻不卡 | 亚洲视频在线免费看| 亚洲伦理中文字幕| 亚洲第一视频在线观看免费| 最新亚洲成av人免费看| 亚洲伊人精品综合在合线| 日本不卡视频免费| 国内永久免费crm系统z在线| 33333在线亚洲| 久久久无码精品亚洲日韩软件| 一级毛片免费观看不卡的| 亚洲依依成人亚洲社区| 亚洲午夜久久久影院伊人| 蜜臀AV免费一区二区三区| 免费国产a理论片| 亚洲一卡2卡3卡4卡国产网站 |