Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.5 kB
1
Indexable
Never
#include <iostream>
#include <string>

// Class template for a generic Pair
template <typename T1, typename T2>
class Pair {
public:
    Pair(const T1& first, const T2& second) : first(first), second(second) {}

    T1 getFirst() const { return first; }
    T2 getSecond() const { return second; }

private:
    T1 first;
    T2 second;
};

// Function template to compare two Pairs
template <typename T1, typename T2>
bool comparePairs(const Pair<T1, T2>& pair1, const Pair<T1, T2>& pair2) {
    return (pair1.getFirst() == pair2.getFirst() && pair1.getSecond() == pair2.getSecond());
}

// Overloaded function to compare two Pairs of different types
template <typename T1, typename T2, typename U1, typename U2>
bool comparePairs(const Pair<T1, T2>& pair1, const Pair<U1, U2>& pair2) {
    return false;
}

// Function template to display a Pair
template <typename T1, typename T2>
void displayPair(const Pair<T1, T2>& pair) {
    std::cout << "First: " << pair.getFirst() << ", Second: " << pair.getSecond() << std::endl;
}

int main() {
    Pair<int, double> pair1(5, 3.14);
    Pair<std::string, char> pair2("Hello", 'A');
    Pair<int, double> pair3(5, 3.14);

    std::cout << "Comparing pair1 and pair2: " << comparePairs(pair1, pair2) << std::endl;
    std::cout << "Comparing pair1 and pair3: " << comparePairs(pair1, pair3) << std::endl;

    displayPair(pair1);
    displayPair(pair2);

    return 0;
}