Thanks all for your help. A true lifeline. However, as mentioned I need to implement a set class. I am mocking the STL Library set_union function in c++. Essentially,the intersection is performed on sets, not on strings. The set class ensures that each element is only stored once. Perhaps if this was written first then writing intersection is somewhat easier.
The set is stored in a vector which has the size() method therefore in the loop i need to replace vectore.length() with vector.size().
Thanks in advance.
Code:
// intersection of this set with another
// Pre: a StringSet object
// Post: returns a new StringSet which is the intersection
// of this with otherset, ie. only the items common to both StringSets
StringSet StringSet::set_intersection(StringSet otherset) //this line cannot change. I only want to pass in 2 objects.
{
// record chars that have already been found to intersect so that we don't
// add them to result more than once
int hits[256];
//memset(hits, 0, sizeof(hits));
string result;
char cur;
for ( int i = 0; i < contents.size(); i++) {
cur = contents[i];
// check if we already found char
if (contents.find(cur) != string::npos) {
if (hits[(int)cur] == 0) {
// found new char
hits[(int)cur] = 1;
result += cur;
} else {
// already found char so don't add to result
}
}
}
return result;
}