Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <map>
  6. #include <sstream>
  7. using namespace std;
  8.  
  9.  
  10. class test1
  11. {
  12. private:
  13. std::string name;
  14. int age;
  15. public:
  16. test1() {};
  17. test1(std::string name, int age)
  18. {
  19. this->name = name;
  20. this->age = age;
  21. }
  22. bool operator < (const test1 & a) const
  23. {
  24. return (this->name < a.name) && (this->age < a.age);
  25. }
  26.  
  27. std::string GetName() const
  28. {
  29. return this->name;
  30. }
  31. int GetAge() const
  32. {
  33. return this->age;
  34. }
  35.  
  36. friend ostream & operator << (ostream & os, const test1 & a)
  37. {
  38. os << "name: " << a.GetName() << " " << "age : " << a.GetAge();
  39. return os;
  40. }
  41. };
  42.  
  43. class test2
  44. {
  45. private:
  46. std::string bag;
  47. std::string color;
  48. public:
  49. test2() {};
  50. test2(std::string bagName, std::string color)
  51. {
  52. this->bag = bagName;
  53. this->color = color;
  54. }
  55. std::string GetBag() const
  56. {
  57. return this->bag;
  58. }
  59. std::string Color() const
  60. {
  61. return this->color;
  62. }
  63. friend ostream & operator << (ostream & os, const test2 & a)
  64. {
  65. os << "name: " << a.GetBag() << " " << "age : " << a.Color();
  66. return os;
  67. }
  68. };
  69.  
  70. int main()
  71. {
  72.  
  73. std::map<test1, test2> myMap;
  74. test1 user1{ "John", 20 };
  75. test2 inventory{ "Daykine", "red" };
  76. myMap.insert(pair<test1, test2>(user1, inventory));
  77.  
  78. test1 user2{ "Victor", 20 };
  79. test2 inventory2{ "Daykine", "blue" };
  80. myMap.emplace(user2, inventory2);
  81.  
  82. test1 user3{ "Victor", 20 };
  83. test2 inventory3{ "Daykine", "blue" };
  84. myMap.emplace(user3, inventory3);
  85.  
  86. for (auto & item : myMap)
  87. {
  88. cout << item.first;
  89. cout << item.second;
  90. }
  91.  
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement