今天看到以前寫的一個關于容器排序以及賦值問題。
先貼以前代碼
#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 () const( return _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來代替手寫循環,
