Shown in the previous section various sorting - Flexible and beautiful mechanism for all occasions. That's just in practice, sort sequence almost never have a pure. This is all from the field of educational and demonstration tasks.
In practice, the more often the problem is sorted quite voluminous data structures (even bulky in size, and according to the number of its fields). And sort (or resorting) they will have on the values for the different fields of these same structures. But here come to the aid STL algorithms, especially when used in conjunction with the functors.
Let's see the typical model problem, which we've seen before - a description of the training group or faculty:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; struct data { // запись о студенте string fio; int group, age, scholarship; inline friend ostream& operator <<(ostream& out, const struct data& obj) { return cout << "[ " << obj.fio << " : " << obj.group << " : " << obj.age << " : " << obj.scholarship << " ]"; } }; struct comp_data { // функтор сравнения int what; bool compare(const struct data& f, const struct data& s) { switch (abs(what)) { case 1: return f.fio < s.fio; case 2: return f.group < s.group; case 3: return f.age < s.age; case 4: return f.scholarship < s.scholarship; default: return false; } } public: comp_data(int what) : what(what) {} bool operator()(const struct data& f, const struct data& s) { bool ret = compare(f, s); return what >= 0 ? ret : !ret; } }; class faculty : public vector<struct data> { // журнал public: faculty(const vector<struct data>& ini) { for (auto &x : ini) this->push_back(x); } inline friend ostream& operator <<(ostream& out, const faculty& obj) { for (auto &x : obj) out << x << endl; return out; } }; int main(void) { setlocale(LC_ALL, "rus"); faculty filology = (vector< struct data >({ { "Сидоров С.С.", 12, 19, 1500 }, { "Иванов И.И.", 13, 20, 0 }, { "Петров П.П.", 11, 21, 0 }, { "Чапаев В.И.", 10, 45, 2000 }, })); while (true) { cout << filology; cout << "поле сортировки? : "; int mode; cin >> mode; if (!cin || (cin.rdstate() & ios::eofbit)) { cout << endl; break; } sort(filology.begin(), filology.end(), comp_data(mode)); } } |
The program requests the field number data, on which sorting will be carried out (actually - comparison). If this number is entered, as a positive number, then sort by that field goes in ascending order. If the number is entered with a minus sign - the sort order is reversed:
Note, that the sorting algorithm does not care that sort: if the numeric data, the largest, and if lowercase, in order leksograficheskom.
And you can similarly interpret the find function(…)?
Tried to write myself a vector search for a structure like
by id element (moreover, there cannot be elements with the same id).
Tried writing a functor like
and send it with an already initialized id (created a functor object with parameters and threw it already) - picked up a bunch of errors like “find functions(★аргументы★) does not exist”.
Tried it the same way with lambda - everything is the same…
I suspect myself of geographically inappropriate hand placement
I can understand and interpret ;-).
Only this is quite detailed., and here the comment editor doesn't allow.
Contact here: http://rus-linux.net/forum/viewforum.php?f=31