Toggle which .cpp
files are being compiled. (Do not include .h
files here.)
xxxxxxxxxx
#include <iostream>
using namespace std;
// Maximum capacity of a set.
const int ELTS_CAPACITY = 10;
class IntSet {
public:
// IntSet constructor - creates an empty set.
IntSet() : elts_size(0) { }
// EFFECTS: returns whether v is in the set
bool contains(int v) const {
return indexOf(v) != -1;
}
// REQUIRES: size() < ELTS_CAPACITY
// EFFECTS: adds v to the set if not already present
void insert(int v) {
// TODO: your code here
}
// EFFECTS: removes v from the set
void remove(int v) {
// (See 2nd exercise below)
}
// EFFECTS: returns the number of elements
int size() const {
return elts_size;
}
// EFFECTS: prints out the set
void print(ostream &os) const {
os << "{" << " ";
if (elts_size > 0) {
os << elts[0];
}
for(int i = 1; i < elts_size; ++i) {
os << ", " << elts[i];
}
os << " }" << endl;
}
private:
int elts[10];
int elts_size;
// EFFECTS: Returns the index of the v in the elts
// array. If not present, returns -1.
int indexOf(int v) const {
for(int i = 0; i < elts_size; ++i){
if(elts[i] == v){
return i;
}
}
return -1;
}
};
ostream &operator<<(ostream &os, const IntSet &s) {
s.print(os);
return os;
}
int main() {
IntSet set;
// Test cases for insert
set.insert(7);
set.insert(32);
set.insert(32);
set.insert(2);
cout << set << endl;
assert(set.size() == 3);
assert(set.contains(7));
assert(set.contains(32));
assert(set.contains(2));
// Test cases for remove
// (See 2nd exercise below)
// set.remove(32);
// assert(set.size() == 2);
// set.remove(4); // does nothing
// assert(set.size() == 2);
// set.remove(32); // does nothing
// assert(set.size() == 2);
// cout << set << endl;
}
Console
cin buffer
Memory
|
|