Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template <class T, class U>
  4. class MyPair
  5. {
  6.     private:
  7.         T key;
  8.         U value;
  9.        
  10.     public:
  11.         MyPair(T key, U value)
  12.         {
  13.             this->key   = key;
  14.             this->value = value;
  15.         }
  16.        
  17.         T get_key() const { return this->key; }
  18.         U get_value() const { return this->value; }
  19.        
  20.         friend std::ostream& operator<<(const std::ostream& os, const MyPair<int, const char*>& pair);
  21.         friend std::ostream& operator<<(const std::ostream& os, const MyPair<bool, int>& pair);
  22. };
  23.  
  24. std::ostream& operator<<(const std::ostream& os, const MyPair<int, const char*>& pair)
  25. {
  26.     return std::cout << "Key: " << pair.get_key() << "\n"
  27.                     << "Value: " << pair.get_value();
  28. }
  29.  
  30. std::ostream& operator<<(const std::ostream& os, const MyPair<bool, int>& pair)
  31. {
  32.     std::ios_base::fmtflags flags = std::cout.flags();
  33.     std::cout.setf(std::ios_base::boolalpha);
  34.     std::cout << "Key: " << pair.get_key() << "\n"
  35.                     << "Value: " << pair.get_value();
  36.    
  37.     std::cout.setf(flags);
  38.     return std::cout;
  39. }
  40.  
  41. int main(int argc, char **argv)
  42. {
  43.     MyPair<int, const char*> pair(50, "Hello :)");
  44.     MyPair<bool, int> pair2(true, 50);
  45.     std::cout << pair << std::endl;
  46.     std::cout << pair2 << std::endl;
  47.    
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement