?
Solved

Mathematical sets using VC++

Posted on 2003-11-30
5
Medium Priority
?
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
efn earned 500 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

764 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