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

            勤能補拙,Expter

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

            一個索引表 (風格,問題推廣分析)


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

            using namespace std;

            template
            < class _RaType>
            struct sort_idxtabl_pair 
            {
                                                                                      
            // 數據容易暴露
                _RaType _ra; 
                
            int     _val;

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

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

            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);                                            // 調用構造函數

                
            int index[10];

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

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

                
                system(
            "pause");
                
            return 0;
            }


            運行結果
             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
            請按任意鍵繼續. . .


            1.分析風格,首先operator < 操作符 應該聲明為const函數
            2.排序函數應該更簡單,很冗余
            3.更多的復用
            比如
            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、在迭代器盡量不要寫 <,應寫!= 來比較


            總結最近學習筆記,以及泛型編程 解決上面問題,以及風格問題:


             

            方案一:
            //  進行一些基本清理工作,運用原來數據結構
            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函數
                {
                 
            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);            // 一個對象數組
               for ( int i = 0 ; i < last - first ; ++i) 
                V[i].SetVal(first
            +i,i);                                                                                  // 調用每個對象方法
              
               sort(V.begin(),V.end());                                                                             
            // 排序
               copy(V.begin(),V.end(),out);                                                                   // 調用copy函數
              }

            }



            方案二:
            // 使用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函數
               {
                 
            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);                    // 對象數組
              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;
              }

            }


            方案三:
            //其實還可以利用multimap來消除單獨排序步驟,利用transform來代替手寫循環,


             

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

            評論

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

            寫得真規范~
            請問
            typedef vector< pair<_RaType,int> > V (last-first); // 對象數組
            就把V創建好了嗎?  回復  更多評論   

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

            @lzmagic

            是的,調用構造函數。。。  回復  更多評論   

            segui久久国产精品| www.久久热.com| 久久久久久精品久久久久| 无码八A片人妻少妇久久| 国产成人无码精品久久久性色| 久久99这里只有精品国产| 丰满少妇高潮惨叫久久久| 久久夜色精品国产| 久久国产色AV免费观看| 午夜精品久久久久成人| 久久久久国产精品| 久久人人爽人人爽人人片av麻烦| 国产精品久久久久久| 久久天天躁狠狠躁夜夜2020一 | 久久中文精品无码中文字幕| 亚洲欧美日韩中文久久 | 国产成人99久久亚洲综合精品| 香蕉久久夜色精品国产尤物| 国产亚州精品女人久久久久久| 久久久无码精品亚洲日韩按摩| 中文字幕精品久久久久人妻| 久久人人爽人人澡人人高潮AV | 国产欧美久久一区二区| 久久精品免费一区二区| 青青草原综合久久大伊人导航| 精品精品国产自在久久高清| 浪潮AV色综合久久天堂| 色老头网站久久网| 亚洲欧美国产日韩综合久久| 很黄很污的网站久久mimi色| 九九久久精品国产| 国产成人无码精品久久久久免费| 久久夜色精品国产亚洲| 91超碰碰碰碰久久久久久综合| 久久天天躁狠狠躁夜夜avapp | 久久精品免费观看| 办公室久久精品| 久久国产美女免费观看精品| 日韩久久无码免费毛片软件| 性欧美大战久久久久久久| 香蕉久久夜色精品国产尤物|