Добавил:
Рад, если кому-то помог Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
0
Добавлен:
01.11.2025
Размер:
2.41 Кб
Скачать
#include <iostream>
#include <vector>
#include <algorithm>
#include <locale>
using namespace std;

template<typename T>
class Set {
private:
    vector<T> elements;

public:
    // Добавление элемента
    void add(const T& element) {
        if (!in(element)) {
            elements.push_back(element);
        }
    }
    
    // Удаление элемента
    void del(const T& element) {
        auto it = find(elements.begin(), elements.end(), element);
        if (it != elements.end()) {
            elements.erase(it);
        }
    }
    
    // Проверка вхождения
    bool in(const T& element) const {
        return find(elements.begin(), elements.end(), element) != elements.end();
    }
    
    // Пересечение множеств
    Set operator*(const Set& other) const {
        Set result;
        for (const auto& elem : elements) {
            if (other.in(elem)) {
                result.add(elem);
            }
        }
        return result;
    }
    
    // Объединение множеств
    Set operator+(const Set& other) const {
        Set result = *this;
        for (const auto& elem : other.elements) {
            result.add(elem);
        }
        return result;
    }
    
    // Разность множеств
    Set operator-(const Set& other) const {
        Set result;
        for (const auto& elem : elements) {
            if (!other.in(elem)) {
                result.add(elem);
            }
        }
        return result;
    }
    
    // Вывод множества
    void print() const {
        cout << "{ ";
        for (const auto& elem : elements) {
            cout << elem << " ";
        }
        cout << "}" << endl;
    }
};

int main() {
    setlocale(LC_ALL, "ru_RU.UTF-8");
    
    Set<int> set1, set2;
    
    set1.add(1);
    set1.add(2);
    set1.add(3);
    
    set2.add(2);
    set2.add(3);
    set2.add(4);
    
    cout << "Множество 1: ";
    set1.print();
    
    cout << "Множество 2: ";
    set2.print();
    
    cout << "Пересечение: ";
    (set1 * set2).print();
    
    cout << "Объединение: ";
    (set1 + set2).print();
    
    cout << "Разность (1-2): ";
    (set1 - set2).print();
    
    return 0;
}
Соседние файлы в папке Лаба8