权重结构的加权排序算法
开始算法之前,首先介绍一下向量中的排序方式,这里使用的是STL中的std::sort排序方式,具体使用的代码如下:
//定义加权排序的结构template struct _sortStru { T _data1; T _data2; T _data3; T _data4; int nWeight[4]; _sortStru() { memset(this, 0, sizeof(_sortStru)); }};bool _sort_sample(const _sortStru &l, const _sortStru &r){ return l._data1 < r._data1;}int main(){ // 初始化一个vector,成员类型为_sortStru vector< _sortStru > vec; int i = 0; for (i = 0; i < MAXNUM; i++) { _sortStru sort; sort._data1 = Random(); sort._data2 = Random(); sort._data3 = Random(); sort._data4 = Random(); vec.push_back(sort); } // 输出 for (i = 0; i < MAXNUM; i++) { _sortStru out; out = vec.at(i); cout<< out._data1<<" "< <<" "< <<" "< <<" "; cout<
out; out = vec.at(i); cout<< out._data1<<" "<
<<" "< <<" "< <<" "; cout< //定义加权排序的结构template struct _sortStru { T _data1; T _data2; T _data3; T _data4; int nWeight[4]; _sortStru() { memset(this, 0, sizeof(_sortStru)); }};
在这个结构中有四个成员变量,分别任_data1 - _data4,这个四个成员都有自己的权重,比如_data1的权重是30,_data2的权重是20,_data3的权重是40,_data4的权重是10。然后根据这些权重对一组数据进行排序。然后将排序后的结果从大到小排列出来。
首先,对这组数据中的每个成员从小到大排序,具体的先对第一个成员进行从小到大排序,排好序后给这些成员添加上自己的位置权重,最小的为0,然后依次增加,循环的给所有的成员都添加上位置权重。
1 #include 2 #include 3 #include 4 #include 5 #define Random() (rand()%100) 6 using namespace std; 7 #define MAXNUM 10 8 9 //定义加权排序的结构 10 template 11 struct _sortStru 12 { 13 T _data1; 14 T _data2; 15 T _data3; 16 T _data4; 17 int nWeight[4]; 18 _sortStru() 19 { 20 memset(this, 0, sizeof(_sortStru)); 21 } 22 }; 23 24 25 bool _sort_sample(const _sortStru &l, const _sortStru &r) 26 { 27 return l._data1 < r._data1; 28 } 29 30 31 class _sort 32 { 33 public: 34 _sort() : pos(0){} 35 _sort(int nPos) : pos(nPos){} 36 bool operator()(const _sortStru &l, const _sortStru &r) 37 { 38 switch (pos) 39 { 40 case 0: 41 return l._data1 < r._data1; 42 case 1: 43 return l._data2 < r._data2; 44 case 2: 45 return l._data3 < r._data3; 46 case 3: 47 return l._data4 < r._data4; 48 default: 49 return l._data1 < r._data1; 50 } 51 } 52 53 private: 54 int pos; 55 }; 56 57 template 58 class Add_Weight 59 { 60 public: 61 Add_Weight(int type, int start) 62 : ntype(type), nstart(start), nLastValue(0), nLastWeight(0) 63 { 64 } 65 66 void operator()(_sortStru &_F) 67 { 68 switch (ntype) 69 { 70 case 0: default: 71 if (_F._data1 == nLastValue) 72 { 73 _F.nWeight[ntype] = nLastWeight; 74 } 75 else 76 { 77 _F.nWeight[ntype] = nstart; 78 nLastValue = _F._data1; 79 nLastWeight = nstart; 80 } 81 break; 82 case 1: 83 if (_F._data2 == nLastValue) 84 { 85 _F.nWeight[ntype] = nLastWeight; 86 } 87 else 88 { 89 _F.nWeight[ntype] = nstart; 90 nLastValue = _F._data2; 91 nLastWeight = nstart; 92 } 93 break; 94 case 2: 95 if (_F._data3 == nLastValue) 96 { 97 _F.nWeight[ntype] = nLastWeight; 98 } 99 else100 {101 _F.nWeight[ntype] = nstart;102 nLastValue = _F._data3;103 nLastWeight = nstart;104 }105 break;106 case 3:107 if (_F._data4 == nLastValue)108 {109 _F.nWeight[ntype] = nLastWeight;110 }111 else112 {113 _F.nWeight[ntype] = nstart;114 nLastValue = _F._data4;115 nLastWeight = nstart;116 }117 break;118 }119 nstart++;120 }121 private:122 int ntype;123 int nstart;124 T nLastValue;125 int nLastWeight;126 };127 128 129 130 // 四个参数的权重类131 class CWeight132 {133 public:134 CWeight()135 {136 weight_1 = 0;137 weight_1 = 0;138 weight_1 = 0;139 weight_1 = 0;140 };141 142 CWeight(int Fir, int Sec, int thi, int Fou)143 : weight_1(Fir), weight_2(Sec), weight_3(thi), weight_4(Fou)144 {145 };146 147 void Check()148 {149 assert(weight_1 + weight_2 + weight_3 + weight_4 == 100);150 }151 152 public:153 int weight_1;154 int weight_2;155 int weight_3;156 int weight_4;157 };158 159 template 160 class Compare_Weight161 {162 public:163 Compare_Weight(CWeight *pF)164 : pweight(pF)165 {166 }167 168 bool operator()(const _sortStru &_F, const _sortStru &_L)169 {170 T t1 = _F.nWeight[0] * pweight->weight_1 171 + _F.nWeight[1] * pweight->weight_2 172 + _F.nWeight[2] * pweight->weight_3173 + _F.nWeight[3] * pweight->weight_4;174 175 T t2 = _L.nWeight[0] * pweight->weight_1 176 + _L.nWeight[1] * pweight->weight_2 177 + _L.nWeight[2] * pweight->weight_3178 + _L.nWeight[3] * pweight->weight_4;179 180 return t1 > t2;181 }182 183 private:184 CWeight *pweight;185 };186 187 int main()188 {189 // 初始化一个vector,成员类型为_sortStru 190 vector< _sortStru > vec;191 int i = 0;192 for (i = 0; i < MAXNUM; i++)193 {194 _sortStru sort;195 sort._data1 = Random();196 sort._data2 = Random();197 sort._data3 = Random();198 sort._data4 = Random();199 vec.push_back(sort);200 }201 202 // 输出203 for (i = 0; i < MAXNUM; i++)204 {205 _sortStru out;206 out = vec.at(i);207 cout<< out._data1<<" "< <<" "< <<" "< <<" ";208 cout<