| 1 |
#include <vector> |
| 2 |
#include <algorithm> |
| 3 |
#include <iostream> |
| 4 |
#include <fstream> |
| 5 |
#include <string> |
| 6 |
#include <iterator> |
| 7 |
|
| 8 |
//SORTWITH |
| 9 |
template <typename Type> |
| 10 |
class SortWith |
| 11 |
{ |
| 12 |
bool (Type::*d_ptr)(Type const &rhv) const; |
| 13 |
//= |
| 14 |
public: |
| 15 |
SortWith(bool (Type::*ptr)(Type const &rhv) const); |
| 16 |
bool operator()(Type const &lhv, Type const &rhv) const; |
| 17 |
}; |
| 18 |
|
| 19 |
//SORTCONS |
| 20 |
template <typename Type> |
| 21 |
SortWith<Type>::SortWith(bool (Type::*ptr)(Type const &rhv) const) |
| 22 |
: |
| 23 |
d_ptr(ptr) |
| 24 |
{} |
| 25 |
//= |
| 26 |
|
| 27 |
//OPERATOR |
| 28 |
template <typename Type> |
| 29 |
bool SortWith<Type>::operator()(Type const &lhv, Type const &rhv) const |
| 30 |
{ |
| 31 |
return (lhv.*d_ptr)(rhv); |
| 32 |
} |
| 33 |
//= |
| 34 |
|
| 35 |
//HEAD |
| 36 |
template <typename Type> |
| 37 |
class SortVector: public std::vector<Type> |
| 38 |
{ |
| 39 |
public: |
| 40 |
SortVector() |
| 41 |
{} |
| 42 |
SortVector(Type const *begin, Type const *end) |
| 43 |
: |
| 44 |
std::vector<Type>(begin, end) |
| 45 |
{} |
| 46 |
//= |
| 47 |
void hierarchicSort |
| 48 |
( |
| 49 |
bool (Type::**arr)(Type const &rhv) const, |
| 50 |
size_t n |
| 51 |
); |
| 52 |
}; |
| 53 |
|
| 54 |
//SORT |
| 55 |
template <typename Type> |
| 56 |
void SortVector<Type>::hierarchicSort( |
| 57 |
bool (Type::**arr)(Type const &rhv) const, |
| 58 |
size_t n) |
| 59 |
{ |
| 60 |
while (n--) |
| 61 |
stable_sort(this->begin(), this->end(), SortWith<Type>(arr[n])); |
| 62 |
} |
| 63 |
//= |
| 64 |
class MultiData; |
| 65 |
namespace std |
| 66 |
{ |
| 67 |
ostream &operator<<(ostream &str, MultiData const &obj); |
| 68 |
istream &operator>>(istream &str, MultiData &obj); |
| 69 |
} |
| 70 |
|
| 71 |
class MultiData |
| 72 |
{ |
| 73 |
friend std::ostream |
| 74 |
&std::operator<<(std::ostream &str, MultiData const &obj); |
| 75 |
friend std::istream |
| 76 |
&std::operator>>(std::istream &str, MultiData &obj); |
| 77 |
|
| 78 |
std::string d_text; |
| 79 |
int d_value; |
| 80 |
|
| 81 |
public: |
| 82 |
bool textCmp(MultiData const &rhv) const |
| 83 |
{ |
| 84 |
return d_text < rhv.d_text; |
| 85 |
} |
| 86 |
bool intCmp(MultiData const &rhv) const |
| 87 |
{ |
| 88 |
return d_value < rhv.d_value; |
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
namespace std |
| 93 |
{ |
| 94 |
ostream &operator<<(ostream &output, MultiData const &obj) |
| 95 |
{ |
| 96 |
return output << obj.d_text << " " << obj.d_value; |
| 97 |
} |
| 98 |
istream &operator>>(istream &input, MultiData &obj) |
| 99 |
{ |
| 100 |
return input >> obj.d_text >> obj.d_value; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
using namespace std; |
| 105 |
|
| 106 |
int main() |
| 107 |
{ |
| 108 |
//MAIN1 |
| 109 |
SortVector<MultiData> sv; |
| 110 |
|
| 111 |
copy(istream_iterator<MultiData>(cin), |
| 112 |
istream_iterator<MultiData>(), |
| 113 |
back_inserter(sv)); |
| 114 |
//= |
| 115 |
copy(sv.begin(), sv.end(), |
| 116 |
ostream_iterator<MultiData>(cout, " ")); |
| 117 |
|
| 118 |
cout << "\n====\n"; |
| 119 |
//ARRAY |
| 120 |
bool (MultiData::*arr[])(MultiData const &rhv) const = |
| 121 |
{ |
| 122 |
&MultiData::textCmp, |
| 123 |
&MultiData::intCmp, |
| 124 |
}; |
| 125 |
//= |
| 126 |
//SORT1 |
| 127 |
sv.hierarchicSort(arr, 2); |
| 128 |
//= |
| 129 |
copy(sv.begin(), sv.end(), |
| 130 |
ostream_iterator<MultiData>(cout, " ")); |
| 131 |
|
| 132 |
cout << "\n====\n"; |
| 133 |
//SORT2 |
| 134 |
swap(arr[0], arr[1]); |
| 135 |
sv.hierarchicSort(arr, 2); |
| 136 |
//= |
| 137 |
copy(sv.begin(), sv.end(), |
| 138 |
ostream_iterator<MultiData>(cout, " ")); |
| 139 |
|
| 140 |
cout << "\n====\n"; |
| 141 |
} |