Advertisement
Guest User

Untitled

a guest
May 27th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #ifndef PAIR_H
  2. #define PAIR_H
  3. #include<iostream>
  4. using namespace std;
  5. #include <cstring>
  6.  
  7.  
  8. template <class T>
  9. class Pair
  10. {
  11. public:
  12. Pair();
  13. virtual ~Pair();
  14. Pair(const char* key,const T &value);
  15. Pair(const Pair &pair);
  16. Pair& operator=(const Pair &pair);
  17.  
  18. bool operator==(const Pair &pair) const;
  19. bool operator!=(const Pair &pair);
  20. template<class U>
  21. friend ostream& operator<<(ostream &os,const Pair<U> &pair)
  22. {
  23. os<<pair.getKey()<< " "<<pair.getValue()<<endl;
  24. return os;
  25. }
  26.  
  27. char* getKey() const;
  28. T getValue() const;
  29. void setValue( T newValue);
  30.  
  31.  
  32. private:
  33. void copyKey(const char* key);
  34. char* key;
  35. T value;
  36.  
  37. };
  38.  
  39.  
  40.  
  41.  
  42. template<class T>
  43. void Pair<T>::copyKey(const char* toCopyKey)
  44. {
  45. if(toCopyKey==NULL)
  46. {
  47. throw std::invalid_argument("Cannot set name equal to NULL"); //restriction in order keys to make sense
  48. }
  49.  
  50. delete[] key;
  51. int length=strlen(toCopyKey);
  52. key=new char[length+1];
  53. strcpy(key,toCopyKey);
  54. }
  55. template<class T>
  56. Pair<T>::Pair() : key(nullptr)
  57. {
  58. copyKey("default key");
  59.  
  60. }
  61.  
  62.  
  63.  
  64. template<class T>
  65. Pair<T>::Pair(const char* key,const T &val ) : key(nullptr)
  66. {
  67. copyKey(key);
  68. setValue(val);
  69.  
  70. }
  71. template<class T>
  72. Pair<T>::~Pair()
  73. {
  74. delete[] key;
  75. }
  76.  
  77. template<class T>
  78. Pair<T>::Pair(const Pair &pair) : key(nullptr)
  79. {
  80.  
  81. copyKey(pair.getKey());
  82. setValue(pair.getValue());
  83.  
  84. }
  85.  
  86. template<class T>
  87. Pair<T>& Pair<T>::operator=(const Pair &pair)
  88. {
  89. if(this != &pair)
  90. {
  91. copyKey(pair.getKey());
  92. setValue(pair.getValue());
  93. }
  94. return *this;
  95. }
  96.  
  97.  
  98. template<>
  99. bool Pair<char*>::operator==(const Pair<char*> &pair) const
  100. {
  101. return strcmp(getKey(),pair.getKey())==0 && strcmp(pair.getValue(),getValue())==0;
  102. }
  103.  
  104. // everything else
  105. template<class T>
  106. bool Pair<T>::operator==(const Pair<T> &pair) const
  107. {
  108. return getValue()==pair.getValue() && strcmp(getKey(),pair.getKey())==0;
  109. }
  110.  
  111.  
  112. template<class T>
  113. bool Pair<T>::operator!=(const Pair<T> &pair)
  114. {
  115. return !(*this==pair);
  116. }
  117.  
  118.  
  119. template<class T>
  120. char* Pair<T>::getKey() const
  121. {
  122. return key;
  123. }
  124.  
  125. template<class T>
  126. T Pair<T>::getValue() const
  127. {
  128. return value;
  129. }
  130.  
  131. template<class T>
  132. void Pair<T>::setValue(T newValue)
  133. {
  134. value=newValue;
  135. }
  136. #endif // PAIR_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement