Представьте, что вы ведете записи о группе спортсменов-рыболовов. Для каждого улова записывайте вид рыб, ее вес, дату, фамилию рыболова и т.д. Отсортируйте и распечатайте записи в соответствии с разными критериями. Подсказка: implace_merge().
Не охота мне с этой задачкой морочит голову, да и структуру не так то просто придумать, ее то придумать просто, а от заполнять ее сложно. От кусок программы пытался делать:
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 |
#include <iostream> using std::cout; using std::endl; #include <string> using std::string; #include <vector> using std::vector; struct ylov { string name; struct fish { string vid; double mass; fish(string v,double m):vid(v),mass(m){} }; vector<fish> v; string data; ylov(string n,string d,vector<fish> v1):name(n),data(d),v(v1){} }; int main() { vector<ylov> gr1; vector<ylov::fish> v; v.push_back(ylov::fish("karac6",3.5)); v.push_back(ylov::fish("krab",3.5)); v.push_back(ylov::fish("rak",3.5)); gr1.push_back(ylov("Petrov","13.12.2013",v)); vector<ylov::fish> v1; v1.push_back(ylov::fish("ruba-rak",3.5)); v1.push_back(ylov::fish("kambala",3.5)); v1.push_back(ylov::fish("lech",3.5)); gr1.push_back(ylov("Ivanov","13.12.2013",v)); //выведем результат for(int i=0;i<gr1.size();i++) { cout <<gr1[i].name<<' '<<gr1[i].data<<endl; for(int j=0;j<gr1[i].v.size();j++) { cout <<gr1[i].v[j].vid<<' '<<gr1[i].v[j].mass<<' '; } cout <<endl; } return 0; } |
А теперь еще щас разберем что такое inplace_merge. Да она две отсортированные последовательности слияет в одну, но практической пользы от нее что то я не вижу, от короче примерчик с ее использованием, не знаю как мы ее могли б применить в данной задаче.
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 |
// inplace_merge example #include <iostream> // std::cout #include <algorithm> // std::inplace_merge, std::sort, std::copy #include <vector> // std::vector int main () { int first[] = {5,10,15,20,25}; int second[] = {50,40,30,20,10}; std::vector<int> v(10); std::vector<int>::iterator it; std::sort (first,first+5); std::sort (second,second+5); it=std::copy (first, first+5, v.begin()); std::copy (second,second+5,it); std::inplace_merge (v.begin(),v.begin()+5,v.end()); std::cout << "The resulting vector contains:"; for (it=v.begin(); it!=v.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } |
Все таки мне на форуме помогли там дали код, но я его переделал чуток. Вот он.
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 |
#include <iostream> using std::cout; using std::endl; #include <algorithm> #include <vector> #include <string> using std::string; struct fish_note { fish_note(const std::string& f, double w, string ft) : fisherman(f), weigth(w), fish(ft) { } std::string fisherman; double weigth; string fish; }; int main() { std::vector<fish_note> fish_notes; fish_notes.push_back(fish_note("first", 10.0, "cod")); fish_notes.push_back(fish_note("second", 20.0, "cod")); fish_notes.push_back(fish_note("third", 30.0, "cod")); fish_notes.push_back(fish_note("first", 10.0, "haddock")); fish_notes.push_back(fish_note("second", 20.0, "haddock")); std::inplace_merge(fish_notes.begin(), fish_notes.begin() + 3, fish_notes.end(),[](const fish_note& s, const fish_note& s1){return (s.weigth<s1.weigth);}); //вывод результатов for(int i=0;i<fish_notes.size();i++) { cout <<fish_notes[i].weigth<<' '; } cout <<endl; } |
[youtube]http://www.youtube.com/watch?v=8SrpJ2tV9co[/youtube]