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

            勤能補(bǔ)拙,Expter

            成都游戲Coder,記錄游戲開發(fā)過程的筆記和心得!

            一個(gè)索引表 (風(fēng)格,問題推廣分析)


            今天看到以前寫的一個(gè)關(guān)于容器排序以及賦值問題。
            先貼以前代碼
            #include <iostream>
            #include 
            <cstdlib>
            #include 
            <vector>
            #include 
            <algorithm>

            using namespace std;

            template
            < class _RaType>
            struct sort_idxtabl_pair 
            {
                                                                                      
            // 數(shù)據(jù)容易暴露
                _RaType _ra; 
                
            int     _val;

                
            bool  operator < (const sort_idxtabl_pair &x)                         // 未聲明未const函數(shù)
                {
                    
            return (*_ra) < (*(x._ra));
                }

                
            void SetVal(_RaType & _r , int v) { _ra = _r ;  _val = v;}             // Set函數(shù)
            }
            ;

            template 
            < class _RaType>
            void Sort_index (_RaType first , _RaType last , int * tbl)
            {
                typedef vector
            < sort_idxtabl_pair<_RaType> > V;

                V vv(last
            -first);                                        // 分配空間
                V::iterator vit = vv.begin();
                _RaType it 
            = first; 
                
            for (int i = 0 ; it <last ; it++,vit++,i++)                  // 重寫
                {
                        (
            *vit).SetVal(it,i);
                }

                                                                                        
            // 排序
                std::sort(vv.begin(),vv.end());

                
            int  *pi= tbl;
                vit 
            = vv.begin();

                
            for ( ; vit < vv.end(); pi++,vit++ )                                    // 賦值
                {
                    
            *pi = (*vit)._val;
                }

            }



            int main()
            {
                
            int arr[] = {10,9,8,7,6,4,2,3,1,5};

                vector
            <int> val(arr,arr+10);                                            // 調(diào)用構(gòu)造函數(shù)

                
            int index[10];

                Sort_index(val.begin(),val.end(),index);

                
            for(int i = 0 ; i <10 ; i++)
                
            {
                    cout 
            << " i = " << i                                                     // 輸出數(shù)據(jù)
                     << " , index[i] = " <<index[i]
                         
            <<" ,arr[index]= "<< arr[index[i]]
                         
            <<endl;
                }

                
                system(
            "pause");
                
            return 0;
            }


            運(yùn)行結(jié)果
             i = 0 , index[i] = 8 ,arr[index]= 1
             i = 1 , index[i] = 6 ,arr[index]= 2
             i = 2 , index[i] = 7 ,arr[index]= 3
             i = 3 , index[i] = 5 ,arr[index]= 4
             i = 4 , index[i] = 9 ,arr[index]= 5
             i = 5 , index[i] = 4 ,arr[index]= 6
             i = 6 , index[i] = 3 ,arr[index]= 7
             i = 7 , index[i] = 2 ,arr[index]= 8
             i = 8 , index[i] = 1 ,arr[index]= 9
             i = 9 , index[i] = 0 ,arr[index]= 10
            請(qǐng)按任意鍵繼續(xù). . .


            1.分析風(fēng)格,首先operator < 操作符 應(yīng)該聲明為const函數(shù)
            2.排序函數(shù)應(yīng)該更簡(jiǎn)單,很冗余
            3.更多的復(fù)用
            比如
            template < class _RaType>
            void Sort_index (_RaType first , _RaType last , int * tbl)
            可以修改為

            template 
            < class _RaType, Out res>
            void Sort_index (_RaType first , _RaType last , res  tbl)

            4、在迭代器盡量不要寫 <,應(yīng)寫!= 來比較


            總結(jié)最近學(xué)習(xí)筆記,以及泛型編程 解決上面問題,以及風(fēng)格問題:


             

            方案一:
            //  進(jìn)行一些基本清理工作,運(yùn)用原來數(shù)據(jù)結(jié)構(gòu)
            namespace s1
            {
              template
            <class _RaType>
              
            class sort_idxtabl_pair{
                    
               
            public:
                 
            void SetVal(_RaType & _r , int v) { _ra = _r ;  _val = v;}
                 
            bool  operator < (const sort_idxtabl_pair &x) const                                 // 聲明為const函數(shù)
                {
                 
            return (*_ra) < (*(x._ra));
                }

                
            operator () constreturn _val);

               
            private:
                
                _RaType _ra;
                
            int     _val;
              }
            ;

              template 
            < class _RaType , class ItOut>
                
            void Sort_index (_RaType first , _RaType last , ItOut tbl)
              
            {
               typedef vector
            < sort_idxtabl_pair<_RaType> > V (last-first);            // 一個(gè)對(duì)象數(shù)組
               for ( int i = 0 ; i < last - first ; ++i) 
                V[i].SetVal(first
            +i,i);                                                                                  // 調(diào)用每個(gè)對(duì)象方法
              
               sort(V.begin(),V.end());                                                                             
            // 排序
               copy(V.begin(),V.end(),out);                                                                   // 調(diào)用copy函數(shù)
              }

            }



            方案二:
            // 使用pair輔助類
            namespace s2
            {
              
            //  比較方法
             template<class _RaType , class Val>
                
            struct sort_idxtabl_pair{
                     
            bool  operator < (const pair<_RaType,Val>& x, const pair<_RaType,Val>& y ) const         // 聲明為const函數(shù)
               {
                 
            return  *x.first < *b.first;
               }

             }
            ;
             
             template 
            < class _RaType , class ItOut>
              
            void Sort_index (_RaType first , _RaType last , ItOut tbl)
             
            {
              typedef vector
            < pair<_RaType,int> > V (last-first);                    // 對(duì)象數(shù)組
              for ( int i = 0 ; i < last - first ; ++i)                                                    // 利用pair來 添加新元素
               V[i]=std:make_pair(first+i,i);
              
              sort(V.begin(),V.end(),sort_idxtabl_pair
            <_RaType,int>());       // 排序
              for (int i = 0 ; i <s.SIZE ; ++i ,++tbl)
                
            *tbl = V[i].second;
              }

            }


            方案三:
            //其實(shí)還可以利用multimap來消除單獨(dú)排序步驟,利用transform來代替手寫循環(huán),


             

            posted on 2009-03-08 14:58 expter 閱讀(1231) 評(píng)論(2)  編輯 收藏 引用

            評(píng)論

            # re: 一個(gè)索引表 (風(fēng)格,問題推廣分析) 2009-04-26 13:51 lzmagic

            寫得真規(guī)范~
            請(qǐng)問
            typedef vector< pair<_RaType,int> > V (last-first); // 對(duì)象數(shù)組
            就把V創(chuàng)建好了嗎?  回復(fù)  更多評(píng)論   

            # re: 一個(gè)索引表 (風(fēng)格,問題推廣分析) 2009-04-26 19:22 expter

            @lzmagic

            是的,調(diào)用構(gòu)造函數(shù)。。。  回復(fù)  更多評(píng)論   


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            成人亚洲欧美久久久久| 国产精品内射久久久久欢欢| 久久免费视频6| 日韩一区二区久久久久久| 国产精品久久久久久久久鸭| 久久亚洲精品人成综合网| 亚洲中文字幕久久精品无码APP| 日本精品一区二区久久久| 久久亚洲中文字幕精品一区四| segui久久国产精品| 国产成人久久777777| 久久99精品九九九久久婷婷| a级毛片无码兔费真人久久| 国内精品久久国产大陆| 久久久久久久尹人综合网亚洲| 久久综合久久综合九色| 97精品国产97久久久久久免费 | 热久久国产欧美一区二区精品| 国内精品久久久久久中文字幕| 国产视频久久| 久久国产热这里只有精品| 久久精品国产精品亚洲下载| 色婷婷久久综合中文久久一本| 伊人伊成久久人综合网777| 亚洲色欲久久久综合网东京热| 久久亚洲AV成人无码电影| 久久久久久久尹人综合网亚洲 | 人妻丰满AV无码久久不卡| 国内精品伊人久久久久av一坑| 精品久久777| 思思久久99热免费精品6| 狠狠色婷婷久久综合频道日韩| 国产成人无码久久久精品一| 91精品国产91久久| 亚洲精品无码久久毛片| 久久人爽人人爽人人片AV| 99热热久久这里只有精品68| 亚洲国产精品狼友中文久久久| 色综合久久无码五十路人妻| 精品久久久久久国产91| 色播久久人人爽人人爽人人片aV|