Solved

Mathematical sets using VC++

Posted on 2003-11-30
5
227 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to convert c++ code to Android App 3 108
Need some help with Microsoft Visual Studio C++ 2003 5 65
C++ mouse_event mouse look 7 96
I could not set window to top 4 20
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

860 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