Solved

Mathematical sets using VC++

Posted on 2003-11-30
5
224 Views
Last Modified: 2010-04-01
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... ;(
0
Comment
Question by:Viktorka
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
efn earned 125 total points
ID: 9847354
The standard library has a set container class that may do what you need.

http://www.sgi.com/tech/stl/set.html

If you need more specific help, I suggest you ask a more specific question.

--efn
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9847691
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
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10286264
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
0
 
LVL 15

Expert Comment

by:efn
ID: 10287654
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now