Solved

# Mathematical sets using VC++

Posted on 2003-11-30
223 Views
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
Question by:Viktorka
• 2

LVL 15

Accepted Solution

efn earned 125 total points
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

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

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

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

## Join & Write a Comment Already a member? Login.

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

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

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

#### Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!