Question
The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal is to create a static implementation of a set.
The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal is to create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Your program must compile. Use test_set.cpp as your test program. What I need is for you to add comments to Set.cpp file
Set.h & Test_Set.cpp is code that is already given. ALL I NEED IS FOR YOU TO ADD COMMENTS TO SET.CPP DESCRIBING EACH FUNCTION:
File: Set.cpp (comment before each function on this one -- this is all I need)
#include "set.h" #include
set::set(size_type initial_capacity) { capacity = initial_capacity; used = 0; data = new value_type[capacity]; }
set::set (const set& source) { capacity = source.capacity; used = source.used; data = new value_type[capacity]; for(int i = 0; i < used; i++) data[i] = source.data[i]; }
set::~set() { capacity = 0; used = 0; delete data; }
void set::insert (const value_type& entry) { bool found = false; for(int i = 0; i < used; i++) if(data[i] == entry) { found = true; break; } if(!found && used < capacity) { data[used] = entry; used++; } }
void set::remove (const value_type& entry) { int pos = -1; for(int i = 0; i < used; i++) if(data[i] == entry) { pos = i; break; } for(int i = pos; i < used-1; i++) data[i] = data[i+1]; used--; }
set::size_type set::size() const { return used; }
bool set::contains (const value_type& entry) const { for(int i = 0; i < used; i++) if(data[i] == entry) return true; return false; }
set set_union (const set& s1, const set& s2) { set temp; for(int i = 0; i < s1.size(); i++) temp.insert(s1.data[i]); for(int i = 0; i < s2.size(); i++) temp.insert(s2.data[i]); return temp; }
set set_intersection (const set& s1, const set& s2) { set temp; for(int i = 0; i < s1.size(); i++) if(s2.contains(s1.data[i])) temp.insert(s1.data[i]); return temp; }
set set_difference (const set& s1, const set& s2) { set temp; for(int i = 0; i < s1.size(); i++) if(!s2.contains(s1.data[i])) temp.insert(s1.data[i]); return temp; }
bool is_subset (const set& s1, const set& s2) { for(int i = 0; i < s1.size(); i++) if(!s2.contains(s1.data[i])) return false; return true; }
bool operator == (const set& s1, const set& s2) { if(is_subset(s1, s2) && is_subset(s2, s1)) return true; return false; }
std::ostream& operator << (std::ostream& output, const set& s) { output << "{ "; for(int i = 0; i < s.size()-1; i++) output < File: Test_Set.cppFILE: SET.H #ifndef _SET_H #define _SET_H #include
#include "set.h" #include
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started