Link to home
Start Free TrialLog in
Avatar of Viktorka
Viktorka

asked on

Mathematical sets using VC++

hi,

i'm new to visual c++, but i need a code really fast, that can handle mathematical sets... i was told to use STL, but i tried using a matrix for storing the sets (each row represents a set), because i don't know using STL ;(

could you help me with a code that uses STL to create/modify sets? i'm completely upset with this matrix solution... ;(
ASKER CERTIFIED SOLUTION
Avatar of efn
efn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bcladd
bcladd

The STL's std::set is fantastic since you can create sets of objects without having to do any bit manipulation (or array addressing) yourself. Also nice because you don't have to reinvent union, intersection, or even symetric difference. The down-side is learning all about iterators (to use the set_ * algorithms); the up-side is you will know all about iterators. Here is a snippet of sample code to get you started:

#include <iostream>
#include <string>
#include <set>
#include <algorithm>

using std::cout;
using std::endl;
using std::string;
using std::set;

enum Color {
  red, orange, yellow, green, blue, indigo, violet, Color_end
};

string ColorName[] = {
  "red", "orange", "yellow", "green", "blue", "indigo", "violet", "PANIC"
};

int main()
{

  set<Color> s1, s2;

  s1.insert(red);
  s1.insert(green);
  s1.insert(violet);

  s2.insert(orange);
  s2.insert(green);
  s2.insert(indigo);

  set<Color> theIntersection;
  set_intersection(s1.begin(), s1.end(),
                   s2.begin(), s2.end(),
                   inserter(theIntersection, theIntersection.begin()));

  string separator = "";
  cout << "theIntersection = {";
  for (set<Color>::const_iterator it = theIntersection.begin();
       it != theIntersection.end();
       ++it) {
    cout << separator << ColorName[*it];
    separator = ", ";
  }
  cout << "}" << endl;
 
  return 0;
}


Hope this helps,
-bcl
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: efn {http:#9847354}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
I think bcladd should get at least some of the points since the question asked for code, bcl provided some, and I didn't.

--efn