Орлов Андрей Сергеевич
09.10.2021 18:50 (24 д. 09:24)
[5464869]
#pragma once
#include <vector>
class Set {
public:
std::vector<int64_t> array;
explicit Set(const std::vector<int64_t> &vec) {
for (auto & i : vec) {
this->Add(i);
}
}
Set() {}
std::vector<int64_t> Data() {
return this->array;
}
void Add(int64_t number) {
bool present = false;
for (int64_t i : this->array) {
if (i == number) {
present = true;
break;
}
}
if (!present) {
this->array.push_back(number);
}
}
void Remove(int64_t number) {
for (unsigned int i = 0; i < this->array.size(); i++) {
if (this->array[i] == number) {
this->array.erase(this->array.begin() + i);
break;
}
}
}
bool Contains(int64_t number) const {
for (int64_t i : this->array) {
if (i == number) {
return true;
}
}
return false;
}
Set Union(const Set& other) {
for (int64_t i : other.array) {
this->Add(i);
}
return *this;
}
Set Intersection(const Set& other) {
for (unsigned int i = 0; i < this->array.size(); i++) {
if (!other.Contains(this->array[i])) {
this->Remove(this->array[i]);
i--;
}
}
return *this;
}
Set Difference(const Set& other) {
for (unsigned int i = 0; i < this->array.size(); i++) {
if (other.Contains(this->array[i])) {
this->Remove(this->array[i]);
i--;
}
}
return *this;
}
Set SymmetricDifference(const Set& other) {
std::vector<int64_t> first;
for (auto & i : this->array){
first.push_back(i);
}
for (unsigned int i = 0; i < this->array.size(); i++){
if (other.Contains(this->array[i])) {
this->Remove(this->array[i]);
i--;
}
}
for (unsigned int i = 0; i < other.array.size(); i++){
bool con = false;
for (auto & j : first){
if (j == other.array[i]){
con = true;
break;
}
}
if (!con){
this->Add(other.array[i]);
}
}
return *this;
}
};