PDF z cvičení (ZS 2021)

template.pdf

template_vs_supertype.pdf

Zdrojové súbory z cvičení (ZS 2021)

// Tema: Komparator
// Autor: Vladislav Novak

#include <iostream>
#include <set>
#include <queue>

using namespace std;

class Number {
private:
    int data;
public:
    explicit Number(int number) : data(number) { }
    int getData() const { return data; }

//    bool operator<(const Number &other) const { // clensky
//        return getData() < other.getData();
//    }
};

bool operator<(const Number &left, const Number & right) { // globalny
    return left.getData() < right.getData();
}

struct NumberComparator {
    // definovany "function call operator" => struktura (trieda) je "FunctionObject" typom
    bool operator()(const Number &left, const Number &right) const {
        return left.getData() < right.getData();
    }
    bool operator()(int a) const {
        return a % 2 == 0;
    }
    string operator()() const {
        return "bez parametrov";
    }
};

int main() {
    Number n1(1);
    Number n2(2);

    // pouzitie operatora <
    cout << "operator< ----------------------------------------------------------------------" << endl;
    cout << (n1 < n2 ? "true" : "false") << endl;
    cout << (n1 < n1 ? "true" : "false") << endl;
    cout << (n2 < n1 ? "true" : "false") << endl;
    cout << endl;

    // pouzitie operatora < pre vytvorenie mnoziny a prioritnej fronty prvkov typu 'Number'
    cout << "s1 -----------------------------------------------------------------------------" << endl;
    set<Number> s1;
    s1.insert(n1);
    s1.insert(n2);

    for(const Number & n: s1) {
        cout << n.getData() << " ";
    }
    cout << endl << endl;

    cout << "pq1 " << endl;
    priority_queue<Number> pq1;
    pq1.push(n1);
    pq1.push(n2);

    while(! pq1.empty()) {
        cout << pq1.top().getData() << " ";
        pq1.pop();
    }
    cout << endl << endl;

    // pouzitie komparatora pre vytvorenie mnoziny a prioritnej fronty prvkov typu 'Number'
    cout << "komparator ---------------------------------------------------------------------" << endl;
    NumberComparator c1;
    cout << c1(n1, n2) << endl;
    cout << c1(n1, n1) << endl;
    cout << c1(n2, n1) << endl;
    cout << c1(10) << endl;
    cout << c1() << endl;
    cout << endl;

    cout << "s2 -----------------------------------------------------------------------------" << endl;
    set<Number, NumberComparator> s2;
    s2.insert(n1);
    s2.insert(n2);

    for(const Number & n: s2) {
        cout << n.getData() << " ";
    }
    cout << endl << endl;

    cout << "pq2" << endl;
    priority_queue<Number, vector<Number>, NumberComparator> pq2;
    pq2.push(n1);
    pq2.push(n2);

    while(! pq2.empty()) {
        cout << pq2.top().getData() << " ";
        pq2.pop();
    }
    cout << endl;

    return 0;
}
// Tema: Template funkcia/trieda
// Autor: Pavol Marak

#include <iostream>
#include <string>
#include <vector>

using namespace std;

// template funkcia
template<typename T>
const T &maximum(const T &a, const T &b) {
    return (a < b) ? b : a;
}

// pomocna trieda
class Car {
    // ...
};

// pomocna trieda
class Computer {

};

#define MAX_ITEMS 100

// template trieda
template<typename T>
class Warehouse {
    T items[MAX_ITEMS];
    size_t item_count;
public:
    void insert(const T &item) { /* TODO */ }

    void remove(const T &item) { /* TODO */ }

    bool isItemAvailable(const T &item) { /* TODO */ }

    size_t itemCount() { /* TODO */ }
    // ...
};

// template trieda reprezentujuca dvojicu prvkov
template<typename T, typename U>
struct MyPair {
    T first;
    U second;

    void about() {
        cout << "Primary template." << endl;
    }
};

// specializovana implementacia template triedy
// pre typ int a std::string
template<>
struct MyPair<int, string> {
    int first;
    string second;

    void about() {
        cout << "Fully specialized template." << endl;
    }
};

// ciastocne specializovana implementacia template triedy
// pre typ T a char
template<typename T>
struct MyPair<T, char> {
    T first;
    char second;

    void about() {
        cout << "Partially specialized template." << endl;
    }
};

// sablona s netypovym parametrom
template<typename T, int value>
void print(const T &a) {
    cout << value << " " << a << endl;
}

int main() {

    // Test template funkcie
    cout << "maximum(5,6) = " << maximum<int>(5, 6) << endl;
    cout << "maximum(\"hello\",\"world\") = " << maximum<string>("hello", "world") << endl;

    // Dedukcia template parametrov (netreba uviest <...>)
    cout << "maximum('c','t') = " << maximum('c', 't') << endl;

    // Test konstrukcie template tried
    Warehouse<Computer> computers{}; // prazdny sklad pocitacov
    Warehouse<Car> cars{}; // prazdny sklad aut

    // Ukazka specializacie sablony

    // a) pouzitie primarnej sablony
    MyPair<string, vector<int>> mp1{"example", {1, 2, 3, 4, 5}};

    // b) pouzitie plne specializovanej sablony pre typ <int,string>
    MyPair<int, string> mp2{111, "have a nice day"};

    // c) pouzitie ciastocne specializovanej sablony pre typ <T,char>
    MyPair<int, char> mp3{22, 'r'};

    mp1.about();
    mp2.about();
    mp3.about();

    // Ukazka pouzitia sablony s netypovym parametrom
    print<string, 999>("abcdefgh");

    return 0;
}