Assignment help - Thrown in the deep end

Hi there. I'm very new to C++ (or any C related language) and have been thrown a rather difficult assignment. The ides is to implement a set class, with functions for unions and intersections of sets. The first file, set.h has been given as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef SET_H
#define SET_H

#include <iostream>
#include <vector>

using namespace std;

class Set
{
	// friend operator functions
	friend ostream &operator<<(ostream & out, const Set & s);
	friend istream &operator>>(istream & in, Set & s);
	
	public:
	// Constructors
	Set(); // default constructor
	Set(const Set & s); // copy constructor
	
	// destructor
	~Set();
	
	// operator functions
	Set operator||(Set & s); // union of the set with the set s
	Set operator&&(Set & s); // intersection of the set with the set s
	const Set& operator=(const Set & s); // assignment 
	bool operator==(const Set & s); // equality
	int& operator[](int index); // returns modifiable lvalue
	int operator[](int index) const; // returns rvalue
	
	// Set membership functions
	bool member(int m); // The integer m is a member
	void add(int m); // add the integer m to the set
	void remove(int m); // remove the integer m from the set
	
	private:
	vector<int> _s;
};
#endif 


So I'm trying (and failing) to write the functions for that class,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "set.h"
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void Set::add(int x)
{
	if (!member(x))
	{
		_s.push_back(x);
	}
}

bool Set::member(int m)
{
	for (int i = 0; i <= _s.size(); i++)
	if (_s.at(m) == true)
	{
		return true;
	}
	return false;
}

void Set::remove(int x)
{
	for (int i = 0; i <= _s.size(); i++)
	if (_s.at(x) == true)
	{
		_s.erase(_s.begin()+i-1);
	}
}

Set Set::operator||(Set & s)
{
	vector<int> vec = s._s;
	
	sort(s._s.begin(), s._s.end());
	sort(_s.begin(), _s.end());
	set_union(_s.begin(), _s.end(), s._s.begin(), s._s.end(),
      	back_inserter(vec));
      	
      s._s = vec;
      return s;
}

Set Set::operator&&(Set & s)
{
	vector<int> vec = s._s;
	
	sort(s._s.begin(), s._s.end());
	sort(_s.begin(), _s.end());
	set_intersection(_s.begin(), _s.end(), s._s.begin(), s._s.end(),
      	back_inserter(vec));
      	
      s._s = vec;
      return s;
}


Now, I'm sure it's full of holes, but I just want to know if I'm headed in the right direction, and if not, how I'm supposed to approach this.

Thanks in advance,
Will.
if (_s.at(m) == true)

std::vector's .at() doesn't return true or false; it returns the character that is there.

As for your union/intersection, I'd rather you use named functions for them instead of overloading. Other than that, you are doing some evil stuff in there. Operators like that shouldn't be modifying their arguments, but you are screwing with s in there so if I do:

1
2
set1 && set2;
//wtf why is set2 different here? 
Thing is, the file set.h was given, so I have no choice but to overload the operators, something I'd really rather avoid doing if I could. I've also had another look at the .at() thing, and I'm now wondering what the heck I was thinking there!
Last edited on
if (_s.at(i) == m) is what you were probably thinking there. The remove logic is also nuts.

BTW, the line above it will go out of bounds so this will throw an exception. You want < not <=.
Last edited on
Topic archived. No new replies allowed.