#ifndef INC_4IMCSCPP_SET_H #define INC_4IMCSCPP_SET_H #include #include class Set { public: Set() = default;; Set(const std::vector&); Set Union(const Set &) const; Set Intersection(const Set &) const; Set Difference(const Set &) const; Set SymmetricDifference(const Set &) const; void Add(int64_t); void Remove(int64_t); bool Contains(int64_t) const; std::vector Data() const; private: std::vector vector; }; Set::Set(const std::vector &vector) { for (auto const item : vector) { this->Add(item); } } Set Set::Union(const Set &old) const { Set new_set; for (auto const item : old.Data()) { new_set.Add(item); } for (auto const item : this->vector) { new_set.Add(item); } return new_set; } Set Set::Intersection(const Set &another) const { Set new_set; for (auto const item1 : this->vector) { for (auto const item2 : another.Data()) { if (item1 == item2) { new_set.Add(item1); } } } return new_set; } Set Set::Difference(const Set &another) const { Set new_set; bool used; for (auto const item1 : this->vector) { used = true; for (auto const item2 : another.Data()) { if (item1 == item2) { used = false; break; } } if (used) { new_set.Add(item1); } } return new_set; } Set Set::SymmetricDifference(const Set &another) const { return this->Difference(another).Union(another.Difference(*this)); } void Set::Add(int64_t num) { for (auto const item : this->vector) { if (item == num) { return; } } this->vector.push_back(num); } void Set::Remove(int64_t num) { for (unsigned int i = 0; i < this->vector.size(); i++) { if (this->vector[i] == num) { this->vector.erase(this->vector.begin() + i); break; } } } bool Set::Contains(int64_t num) const { for (auto const item : this->vector) { if (item == num) { return true; } } return false; } std::vector Set::Data() const { return this->vector; } #endif