Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<class T> class Set {
- list<T> elems;
- public:
- Set() {}
- Set(T val)
- {
- elems = list<T>();
- elems.push_back(val);
- }
- Set(const Set<T>& s) {
- elems = list<T>(s.elems);
- }
- bool isEmpty() const
- {
- return elems.empty();
- }
- Set<T> Union(const Set<T>& r) const {
- Set<T> res(*this);
- for each(T val in r.elems) {
- if (!contains(val))
- res.elems.push_front(val);
- }
- return res;
- }
- Set<T> intersection(const Set<T>& r) const {
- Set<T> res;
- for each (T val in elems) {
- if (r.contains(val))
- res.elems.push_front(val);
- }
- return res;
- }
- Set<T> subtract(const Set<T>& r) const {
- Set<T> res;
- for each (T val in elems) {
- if (!r.contains(val))
- res.elems.push_front(val);
- }
- return res;
- }
- bool contains(const T val) const {
- for each (T val2 in elems) {
- if (val == val2)
- return true;
- }
- return false;
- }
- bool contains(const Set<T> r) const {
- for each(T val in r) {
- if (!contains(val))
- return false;
- }
- return true;
- }
- bool insert(const T val) {
- if (!contains(val)) {
- elems.push_front(val);
- return true;
- } else {
- return false;
- }
- }
- bool erase(const T val) {
- for (auto it = elems.begin(); it != elems.end(); it++) {
- if (*it == val) {
- elems.erase(it);
- return true;
- }
- }
- return false;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement