fork(2) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <iterator>
  5. #include <vector>
  6. #include <string>
  7. using namespace std;
  8.  
  9. class tropical
  10. {
  11. public:
  12. string name;
  13. int price;
  14. };
  15.  
  16. bool sortByName(const tropical &t1, const tropical &t2); //
  17.  
  18. void displayfruits(vector<tropical>& fruitlist)
  19. {
  20. cout << "Name \t\tPrice" << endl << "==========\t=====" << endl;
  21. for(int i=0;i<10;i++) //displays all fruits' names and prices
  22. {
  23. cout << fruitlist[i].name << " \t" << fruitlist[i].price << endl;
  24. }
  25. cout << endl;
  26. }
  27.  
  28. int main()
  29. {
  30. int sortchoice;
  31. string searchfruit;
  32.  
  33.  
  34. string fruitname[] = {"Avocado", "Papaya", "Grapefruit", "Pineapple", "Jackfruit", "Orange", "Honeydew", "Mangosteen", "Banana", "Durian"};
  35. int fruitprice[] = {1, 4, 6, 2, 10, 3, 9, 7, 5, 8};
  36.  
  37. vector<tropical> fruitlist;
  38. tropical fruit;
  39. vector<tropical>::iterator it;
  40.  
  41. for(int i=0; i<10; i++)
  42. {
  43. fruit.name = fruitname[i];
  44. fruit.price = fruitprice[i];
  45.  
  46. fruitlist.push_back(fruit);
  47. }
  48.  
  49. displayfruits(fruitlist);
  50.  
  51. sort (fruitlist.begin(), fruitlist.end(), sortByName);
  52.  
  53. displayfruits(fruitlist);
  54. }
  55.  
  56. bool sortByName(const tropical &t1, const tropical &t2)
  57. {
  58. return t1.name < t2.name;
  59. }
Success #stdin #stdout 0s 3436KB
stdin
Standard input is empty
stdout
Name 		Price
==========	=====
Avocado  	1
Papaya  	4
Grapefruit  	6
Pineapple  	2
Jackfruit  	10
Orange  	3
Honeydew  	9
Mangosteen  	7
Banana  	5
Durian  	8

Name 		Price
==========	=====
Avocado  	1
Banana  	5
Durian  	8
Grapefruit  	6
Honeydew  	9
Jackfruit  	10
Mangosteen  	7
Orange  	3
Papaya  	4
Pineapple  	2