Hi,
I posted here before, but unfortunately I had some issues going on and
was unable to follow up the response. I really appreciate the
responses, and apologize for the absence of a feedback.
Now I writing a code whose (school stuff) whose goal is to deal with
union, intersection and difference of strings. I am having some
trouble with the difference because I have the right index in an int
array but they repeat themselves. I need to get rid of the repetition.
Below is the content of the file that contain the member functions
that are the guts of the program. My problem is only with difference
member function, the index array has values such as: index[1]=2,
index[2]=2, index[3]=2, index[4]=3, index[5]=3, etc. I am trying to
get rid of the repetition.
/ This predefined .cpp file provides the definition of the
// functions members pertaning to the class Set defined on
// the header file proj06.Set.h
#include "proj06.Set.h" //Embed the user defined headerfile
#include <cstring>
#include <string>
#include <vector>
#include <set>
// default constructor
Set::Set(){}
// one string constructor
Set::Set(string InString)
{
string::size_type Loc;
Contents= "";
// Get rid of the duplicate letters.
for (int i = 0; i < InString.size(); i++) {
Loc = Contents.find(InString[i]);
if (Loc == string::npos) {
Contents.push_back(InString.at(i));
}
}
}
// union member function
Set Set::Union(Set s1)
{
string temp=" ";
temp = Contents + s1.Contents; // seems as if the concatenation got
rid of the repeated characters, and it worked
//for (int x = 0; x <= Contents.size()-1; x++)
return (temp); // return temp with the concatenation and result of the
union thereof
}
// intersection member function
Set Set::Intersection(Set s1)
{
int count(0);
string temp=" ";
char ans;
for (int x=0; x <= Contents.size()-1; x++)
for (int n=0; n <= s1.Contents.size()-1; n++)
{
if(Contents.at(x) == s1.Contents.at(n))
{
// cout << Contents.at(x);
temp.at(count) = Contents.at(x);
count = count + 1;
}
}
// cout << " " << temp << " ";
// return (temp);
return (temp);
}
// difference member function
Set Set::Difference(Set s1)
{
int index[10], index2[10];
int count(0), count2(0);
//string temp=" ";
string temp=" ";
for (int x=0; x < Contents.size()-1; x++)
{
index[count]= Contents.find_first_not_of(s1.Contents, x);
count = count + 1;
}
for (int x=0; x < count; x++)
{
if((index[x] == index[x]) && (index2[x] != index[x+1]))
{
index2[count2]=index[x];
count2=count2 + 1;
}
}
for(int x = 0; x < count2; x++)
cout << index2[x] << " ";
return (temp);
}
// search member function
bool Set::Search(char letter)
{
return true;
}
// write member function
void Set::write()
{
cout << Contents; // Print output of private variable or member variable
}