posts - 30,  comments - 28,  trackbacks - 0

          ???? 很經(jīng)典的"轉(zhuǎn)圈數(shù)數(shù)踢人"問題,老早以前做的東西
          ???? 這里是我的解法,利用STL里的List構(gòu)建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

          實(shí)現(xiàn)文件:
          //: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有很高的效率和安全性,并且通用性也比較好
          ???? 這里也發(fā)現(xiàn)個問題,就是STL里的Iterator 還不夠"聰明",這也是為什么我會在程序里多加個迭代器:tmp,
          因?yàn)槿绻艺{(diào)用了it.erase();那么這個迭代器就丟失了,程序也就無非運(yùn)行下去.
          ???? 這樣我們很容易地想,如果調(diào)用了it.erase()后,指針不是丟失,而是指向下一個元素該多好啊!
          ???? 很遺憾,現(xiàn)階段標(biāo)準(zhǔn)STL庫里的List沒有這個功能.猜想是因?yàn)長ist的物理存儲結(jié)構(gòu)的緣故,如果讓他的迭代器變得如我們想像得那么"聰明",那么花費(fèi)可能會有些大.
          ??? 還沒有驗(yàn)證過boost庫,不知道那里的迭代器是否會更人性化些
          ???? 有時間研究一下相關(guān)的STL源碼,寫一個"聰明些"的迭代器.

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

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


          網(wǎng)站導(dǎo)航:
           
          <2006年4月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

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

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          相冊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 湄潭县| 昭觉县| 阿坝县| 公主岭市| 偏关县| 朝阳区| 龙胜| 上栗县| 麻江县| 平山县| 来凤县| 大港区| 惠来县| 重庆市| 隆化县| 新巴尔虎右旗| 土默特右旗| 蓬莱市| 含山县| 临猗县| 香港 | 望江县| 株洲县| 镇平县| 桐城市| 油尖旺区| 三原县| 东至县| 太保市| 贺州市| 满洲里市| 武功县| 余江县| 广南县| 鄂托克前旗| 高陵县| 乌拉特中旗| 澄江县| 古浪县| 黄骅市| 武川县|