Set operators

B

bumbala

I am new to C++. I used mostly Delphi in the past. In Delphi, there was
sets. For example:

TMyType = (nOne, nTwo, nThree);

var MyNumber: TMyType;
if MyNumber in [nOne, nTwo] then blah blah...

What is the equivalent in C++ ?

Thanks.
 
R

Rolf Magnus

I am new to C++. I used mostly Delphi in the past. In Delphi, there was
sets. For example:

TMyType = (nOne, nTwo, nThree);

var MyNumber: TMyType;
if MyNumber in [nOne, nTwo] then blah blah...

What is the equivalent in C++ ?

Not sure, but I guess you're searching something like:

enum TMyType { nOne, nTwo, nThree };

TMyType MyNumber;

if (MyNumber == nOne || MyNumber == nTwo) { blah blah }
 
B

Ben Radford

I am new to C++. I used mostly Delphi in the past. In Delphi, there was
sets. For example:

TMyType = (nOne, nTwo, nThree);

var MyNumber: TMyType;
if MyNumber in [nOne, nTwo] then blah blah...

What is the equivalent in C++ ?

Thanks.

There is no support for sets natively (or in the STL afaik) so you'll
have to find a 3rd party library or roll your own, as with so many
things in C++. I'm a bit busy atm but if you don't have a better answer
when I check back later I'll made a little 'class Set' for you that
provides set support.
 
H

Henryk

Actually these sets are just some bits AFAIK (try to build a set with
more then 32 entries...)

So basically you can define some Symbols like

#define nOne 0x0001 // bit 1
#define nTwo 0x0002 // bit 2
#define nThree 0x0004 // bit 3

Then you can do checks like the delph ... in [...] like this

int MyNumber;
....

if (MyNumber & [nOne | nTwo])
blah blah...

That's the same that happens in delphi "behind the scenes" (AFAIK).

Now you can go on and wrap this all in a nice class...
 
M

Marcus Kwok

Ben Radford said:
I am new to C++. I used mostly Delphi in the past. In Delphi, there was
sets. For example:

TMyType = (nOne, nTwo, nThree);

var MyNumber: TMyType;
if MyNumber in [nOne, nTwo] then blah blah...

What is the equivalent in C++ ?

Thanks.

There is no support for sets natively (or in the STL afaik) so you'll
have to find a 3rd party library or roll your own, as with so many
things in C++. I'm a bit busy atm but if you don't have a better answer
when I check back later I'll made a little 'class Set' for you that
provides set support.

#include <iostream>
#include <set>

enum MyType {One, Two, Three};

int main()
{
std::set<MyType> mySet;

mySet.insert(One);
mySet.insert(Two);


MyType myNumber = One;

if (mySet.find(myNumber) != mySet.end()) {
std::cout << "myNumber was found\n";
}
else {
std::cout << "myNumber was not found\n";
}


myNumber = Three;

if (mySet.find(myNumber) != mySet.end()) {
std::cout << "myNumber was found\n";
}
else {
std::cout << "myNumber was not found\n";
}

return 0;
}
 
B

Ben Radford

Marcus Kwok wrote:
#include <set>

enum MyType {One, Two, Three};

int main()
{
std::set<MyType> mySet;

mySet.insert(One);
mySet.insert(Two);


MyType myNumber = One;

if (mySet.find(myNumber) != mySet.end()) {
std::cout << "myNumber was found\n";
}
else {
std::cout << "myNumber was not found\n";
}


myNumber = Three;

if (mySet.find(myNumber) != mySet.end()) {
std::cout << "myNumber was found\n";
}
else {
std::cout << "myNumber was not found\n";
}

return 0;
}

Ah, so there is STL support for sets, I stand corrected =).
 
M

Marcus Kwok

Ben Radford said:
Marcus Kwok wrote:


Ah, so there is STL support for sets, I stand corrected =).

Yes, it essentially is like a std::map<> that only cares about the keys,
and not the values. There is also a std::multiset<> if you want to be
able to store multiple identical elements in the set.
 
J

Jeff Flinn

Marcus said:
Ben Radford said:
I am new to C++. I used mostly Delphi in the past. In Delphi, there
was sets. For example:

TMyType = (nOne, nTwo, nThree);

var MyNumber: TMyType;
if MyNumber in [nOne, nTwo] then blah blah...

What is the equivalent in C++ ?

Thanks.

There is no support for sets natively (or in the STL afaik) so you'll
have to find a 3rd party library or roll your own, as with so many
things in C++. I'm a bit busy atm but if you don't have a better
answer when I check back later I'll made a little 'class Set' for
you that provides set support.

#include <iostream>
#include <set>

enum MyType {One, Two, Three};

int main()
{
std::set<MyType> mySet;

mySet.insert(One);
mySet.insert(Two);


MyType myNumber = One;

if (mySet.find(myNumber) != mySet.end()) {

if( mSet.count(myNumber) ){
std::cout << "myNumber was found\n";
}
else {
std::cout << "myNumber was not found\n";
}


myNumber = Three;

if (mySet.find(myNumber) != mySet.end()) {


if( mSet.count(myNumber) ){
std::cout << "myNumber was found\n";
}
else {
std::cout << "myNumber was not found\n";
}

return 0;
}

Which is a little more readable, and closer to "contains".

Jeff Flinn
 
M

Marcus Kwok

Jeff Flinn said:
if( mSet.count(myNumber) ){

Which is a little more readable, and closer to "contains".

Thanks. I don't use sets very often, so I was just applying my usual
map usage to sets.
 
R

REH

I am new to C++. I used mostly Delphi in the past. In Delphi, there was
sets. For example:

TMyType = (nOne, nTwo, nThree);

var MyNumber: TMyType;
if MyNumber in [nOne, nTwo] then blah blah...

What is the equivalent in C++ ?

Thanks.

You are welcome to use mine:

http://www.richherrick.com/software/herrick_library.html

example:
enum color_type
{NON_COLOR = -1, RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET,
NUM_COLORS};

typedef herrick::pascal_set<color_type, NUM_COLORS> color_set;

color_set rgb(RED, BLUE, GREEN);

or

const color_set additive_primaries =
color_set::setof<RED, GREEN, BLUE>::value;

const color_set subtractive_primaries =
color_set::setof<RED, YELLOW, BLUE>::value;
 
H

Henryk

Marcus said:
Ben Radford said:
I am new to C++. I used mostly Delphi in the past. In Delphi, there was
sets. For example:

TMyType = (nOne, nTwo, nThree);

var MyNumber: TMyType;
if MyNumber in [nOne, nTwo] then blah blah...

What is the equivalent in C++ ?

Thanks.

There is no support for sets natively (or in the STL afaik) so you'll
have to find a 3rd party library or roll your own, as with so many
things in C++. I'm a bit busy atm but if you don't have a better answer
when I check back later I'll made a little 'class Set' for you that
provides set support.

#include <iostream>
#include <set>

enum MyType {One, Two, Three};

int main()
{
std::set<MyType> mySet;

mySet.insert(One);
mySet.insert(Two);


MyType myNumber = One;

if (mySet.find(myNumber) != mySet.end()) {
std::cout << "myNumber was found\n";
}
else {
std::cout << "myNumber was not found\n";
}


myNumber = Three;

if (mySet.find(myNumber) != mySet.end()) {
std::cout << "myNumber was found\n";
}
else {
std::cout << "myNumber was not found\n";
}

return 0;
}

Your program will take like for ever ... ;o)

This is why my old school c programmer colleagues are so sceptical
about the performance of C++ and STL. You're never quite sure what's
going on under the hood of STL (unless you have some years experience).


The STL code looks nice and fancy but using lengthy loops where some
simple bit tests could do the same is not really efficient...

Cheers
 
C

Clark S. Cox III

Marcus said:
Ben Radford said:
(e-mail address removed) wrote:
I am new to C++. I used mostly Delphi in the past. In Delphi, there was
sets. For example:

TMyType = (nOne, nTwo, nThree);

var MyNumber: TMyType;
if MyNumber in [nOne, nTwo] then blah blah...

What is the equivalent in C++ ?

Thanks.


There is no support for sets natively (or in the STL afaik) so you'll
have to find a 3rd party library or roll your own, as with so many
things in C++. I'm a bit busy atm but if you don't have a better answer
when I check back later I'll made a little 'class Set' for you that
provides set support.

#include <iostream>
#include <set>

enum MyType {One, Two, Three};

int main()
{
std::set<MyType> mySet;

mySet.insert(One);
mySet.insert(Two);


MyType myNumber = One;

if (mySet.find(myNumber) != mySet.end()) {
std::cout << "myNumber was found\n";
}
else {
std::cout << "myNumber was not found\n";
}


myNumber = Three;

if (mySet.find(myNumber) != mySet.end()) {
std::cout << "myNumber was found\n";
}
else {
std::cout << "myNumber was not found\n";
}

return 0;
}

Your program will take like for ever ... ;o)

This is why my old school c programmer colleagues are so sceptical
about the performance of C++ and STL. You're never quite sure what's
going on under the hood of STL (unless you have some years experience).


The STL code looks nice and fancy but using lengthy loops where some
simple bit tests could do the same is not really efficient...

If you want simple bit tests, look at std::bitset.

#include <iostream>
#include <bitset>

enum MyType {One, Two, Three, Max};

typedef std::bitset<Max> MyTypeSet;

int main()
{
MyTypeSet mySet;
mySet[One] = true;
mySet[Two] = true;

MyType myNumber = One;

if (mySet[myNumber])
{
std::cout << "myNumber was found\n";
}
else
{
std::cout << "myNumber was not found\n";
}

myNumber = Three;

if (mySet[myNumber])
{
std::cout << "myNumber was found\n";
}
else
{
std::cout << "myNumber was not found\n";
}

return 0;
}
 
R

REH

Henryk said:
Your program will take like for ever ... ;o)

This is why my old school c programmer colleagues are so sceptical
about the performance of C++ and STL. You're never quite sure what's
going on under the hood of STL (unless you have some years experience).


The STL code looks nice and fancy but using lengthy loops where some
simple bit tests could do the same is not really efficient...

Cheers

That is a very naive way to approach software development. What
implementation is appropiate or "efficient" depends on the
circumstances. Bit tests are good if your information fits in a bit.
What if you have a set of objects? If you want a set that utilizes
bits, they are out there. The STL even has a couple, depending on
whether your set has a constant size or not. As for knowing what's
"under the hood," that's just silly. Do you know what's going on
under the hood of every library you use? Your C Library? Your OS?
Your compiler? The STL, like all good libraries, has a defined
interface. It even defines Big-O guarantees. It is tested and
portable. Re-invent the wheel when efficiency BECOMES a problem, not
before. I use C++ and the STL in real-time embedded systems without
any "efficiency" problems.

REH
 
H

Henryk

REH said:
That is a very naive way to approach software development. What
implementation is appropiate or "efficient" depends on the
circumstances. Bit tests are good if your information fits in a bit.
What if you have a set of objects? If you want a set that utilizes
bits, they are out there. The STL even has a couple, depending on
whether your set has a constant size or not. As for knowing what's
"under the hood," that's just silly. Do you know what's going on
under the hood of every library you use? Your C Library? Your OS?
Your compiler? The STL, like all good libraries, has a defined
interface. It even defines Big-O guarantees. It is tested and
portable. Re-invent the wheel when efficiency BECOMES a problem, not
before. I use C++ and the STL in real-time embedded systems without
any "efficiency" problems.

REH

The OP asked for something equivalent to the Delphi sets. These are in
fact just bits with some & and | operations. No objects and stuff.

And I don't say that the STL is a performance killer. But it needs a
bit of experience to not be blended by all the new features and then
use it the "wrong" way just because you want to use as much STL as
possible. I know what I'm talking about... ;o)

If you have some C and C++ background (like me and my colleages) you
pretty much know what's going on when you iterate over an array, do
some logic operations and other "simple" stuff. You can almost see the
assembly that will be generated.

Using vectors and all these neat things adds a new level of
"uncertainty". You need your time to learn what really happens. That
was my point.
 
R

REH

Henryk said:
And I don't say that the STL is a performance killer. But it needs a
bit of experience to not be blended by all the new features and then
use it the "wrong" way just because you want to use as much STL as
possible. I know what I'm talking about... ;o)

I think you meant "blinded." If so, I agree but that is also true if any
technology you are inexperienced with.
If you have some C and C++ background (like me and my colleages) you
pretty much know what's going on when you iterate over an array, do
some logic operations and other "simple" stuff. You can almost see the
assembly that will be generated.

If you have a C++ background, I believed that is also true for that
language. I also do a lot of Ada development. I could say the same for
that language. I believe an experienced Fortran developer would say the
same.
Using vectors and all these neat things adds a new level of
"uncertainty". You need your time to learn what really happens. That
was my point.
I see your point. I just don't understand what you feel is uncertain about
how a vector behaves.

REH
 
G

Gavin Deane

Henryk said:
If you have some C and C++ background (like me and my colleages) you
pretty much know what's going on when you iterate over an array, do
some logic operations and other "simple" stuff. You can almost see the
assembly that will be generated.

If you want to see the assembly being generated, you can always write
it yourself :)
Using vectors and all these neat things adds a new level of
"uncertainty". You need your time to learn what really happens.

Only if you care. The nature of your programming task may imply that
any time spent thinking at the level of machine instructions is wasted.

Gavin Deane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top