Solved

Mathematical sets using VC++

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 77
How to convert MFC::CString to UTF8 wchar_t* 10 201
Arduino EDI - Programming language 3 91
DCT of  2D array using fftw in c++ 9 38
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

805 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