fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <string>
  5.  
  6. struct element_type
  7. {
  8. double value;
  9. std::string strings[2];
  10. };
  11.  
  12. std::vector<element_type> elements =
  13. {
  14. { 100.0, { "aaaaaaaaaaaa", "bbbbbbbbbbbb" } },
  15. { 200.0, { "cccccccccccc", "dddddddddddd" } },
  16. { 300.0, { "eeeeeeeeeeee", "ffffffffffff" } },
  17. { 400.0, { "hhhhhhhhhhhh", "gggggggggggg" } }
  18. };
  19.  
  20. std::ostream& operator<<(std::ostream& os, const element_type& e)
  21. {
  22. os << std::fixed << std::setprecision(2) << e.value << ' '
  23. << e.strings[0] << ' ' << e.strings[1];
  24. return os;
  25. }
  26.  
  27. template <typename container_type>
  28. void display(std::ostream& os, const container_type& container)
  29. {
  30. for (unsigned i = 0; i < container.size(); ++i)
  31. os << i << ' ' << container[i] << '\n';
  32. os << '\n';
  33. }
  34.  
  35. int main()
  36. {
  37. display(std::cout, elements);
  38.  
  39. unsigned index;
  40. std::cout << "Enter element you'd like to remove.\n> ";
  41.  
  42. if (std::cin >> index && index < elements.size())
  43. {
  44. std::cout << "Removing element " << index << ".\n";
  45. elements.erase(elements.begin() + index);
  46. display(std::cout, elements);
  47. }
  48. else
  49. std::cout << "Unable to remove element " << index << ".\n";
  50. }
Success #stdin #stdout 0s 3480KB
stdin
2
stdout
0 100.00 aaaaaaaaaaaa bbbbbbbbbbbb
1 200.00 cccccccccccc dddddddddddd
2 300.00 eeeeeeeeeeee ffffffffffff
3 400.00 hhhhhhhhhhhh gggggggggggg

Enter element you'd like to remove.
> Removing element 2.
0 100.00 aaaaaaaaaaaa bbbbbbbbbbbb
1 200.00 cccccccccccc dddddddddddd
2 400.00 hhhhhhhhhhhh gggggggggggg