Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- template <class T, class U>
- class MyPair
- {
- private:
- T key;
- U value;
- public:
- MyPair(T key, U value)
- {
- this->key = key;
- this->value = value;
- }
- T get_key() const { return this->key; }
- U get_value() const { return this->value; }
- friend std::ostream& operator<<(const std::ostream& os, const MyPair<int, const char*>& pair);
- friend std::ostream& operator<<(const std::ostream& os, const MyPair<bool, int>& pair);
- };
- std::ostream& operator<<(const std::ostream& os, const MyPair<int, const char*>& pair)
- {
- return std::cout << "Key: " << pair.get_key() << "\n"
- << "Value: " << pair.get_value();
- }
- std::ostream& operator<<(const std::ostream& os, const MyPair<bool, int>& pair)
- {
- std::ios_base::fmtflags flags = std::cout.flags();
- std::cout.setf(std::ios_base::boolalpha);
- std::cout << "Key: " << pair.get_key() << "\n"
- << "Value: " << pair.get_value();
- std::cout.setf(flags);
- return std::cout;
- }
- int main(int argc, char **argv)
- {
- MyPair<int, const char*> pair(50, "Hello :)");
- MyPair<bool, int> pair2(true, 50);
- std::cout << pair << std::endl;
- std::cout << pair2 << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement