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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include <iostream>
#include <set>
#include <iterator>
#include <algorithm>
using namespace std;
//======================================================================
template <typename T> set<T> operator +( const set<T> &A, const set<T> &B ) // Union
{
set<T> result = A;
for ( T e : B ) result.insert( e );
return result;
}
//======================================================================
template <typename T> set<T> operator *( const set<T> &A, const set<T> &B ) // Intersection (sorry, can't think of a better operator)
{
set<T> result;
for ( T e : A ) if ( B.count( e ) ) result.insert( e );
return result;
}
//======================================================================
template <typename T> void listSet( const string &title, const set<T> &S )
{
cout<< title << '\n';
for ( T e : S ) cout << e << '\n';
cout << '\n';
}
//======================================================================
set<string> split( string S, string delim )
{
string part;
set<string> result;
for ( char c : S )
{
if ( delim.find( c ) != string::npos )
{
if ( part != "" ) result.insert( part );
part = "";
}
else
{
part += c;
}
}
if ( part != "" ) result.insert( part );
return result;
}
//======================================================================
int main()
{
string Ustring = "{1,2,9,56,a,Z,99.5 }";
string Astring = "{1,2,a,Z}";
string Bstring = "{99,56,a,Z}";
string delim = "{}, "; // Choose what you like as endstops, blank space or delimiters
set<string> U = split( Ustring, delim );
set<string> A = split( Astring, delim );
set<string> B = split( Bstring, delim );
listSet( "Set U:", U );
listSet( "Set A:", A );
listSet( "Set B:", B );
listSet( "Set AuB:", A + B );
listSet( "Set AnB:", A * B );
if ( U + A == U ) cout << "A is a subset of U" << endl;
else cout << "A is not a subset of U" << endl;
}
//======================================================================
|