Implementation of set class in C++
I have written a function that emulates set_intersection somewhat, with a few teething problems.
Firstly, here is my main code:
*******************************************
Code:
#include <cstdlib>
#include <iomanip>
#include <string>
#include <iostream>
#include <string>
using namespace std;
string stringSet = "ABC";
string otherset = "AC";
string commElmts;
string result;
void set_intersection(string , string );
int main()
{
set_intersection(stringSet, otherset);
system("PAUSE");
}
void set_intersection(string stringSet, string otherset)
{
for(int i = 0; i < otherset.length(); i++)
{
if( stringSet.find( otherset[i] ) != string::npos )
result =result + otherset[i];
commElmts = result;
}
cout<< commElmts<<endl;
};
*************************************
My first problem arrises when say, stringSet = "ABC" and otherset =
"AACD". My output is; "AAC". How do I ensure that a commom element is only outputted once? I think I must implement the class set, I must write the code myself, not use the STL library, which will allow me to store each character once. Is there anyway of modifying my code to do this or alternatively, how do i implement the set class?
I appreciate your help,
Ilan