473,386 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Set operators

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.

Feb 2 '06 #1
16 1855
bu*****@gmail.com 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++ ?


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

enum TMyType { nOne, nTwo, nThree };

TMyType MyNumber;

if (MyNumber == nOne || MyNumber == nTwo) { blah blah }
Feb 2 '06 #2
bu*****@gmail.com 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.

--
Ben Radford
"Why is it drug addicts and computer aficionados are both called users?"
Feb 2 '06 #3
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...

Feb 2 '06 #4
Ben Radford <be**************@new.ox.ac.uk> wrote:
bu*****@gmail.com 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;
}

--
Marcus Kwok
Feb 2 '06 #5

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


Mixed up delphi and C ...

Correct is:

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

--

Henryk

Feb 2 '06 #6
Marcus Kwok wrote:
<snip>
#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 =).

--
Ben Radford
"Why is it drug addicts and computer aficionados are both called users?"
Feb 2 '06 #7
Ben Radford <be**************@new.ox.ac.uk> wrote:
Marcus Kwok wrote:
<snip>
#include <set>


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.

--
Marcus Kwok
Feb 2 '06 #8
Marcus Kwok wrote:
Ben Radford <be**************@new.ox.ac.uk> wrote:
bu*****@gmail.com 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()) {


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
Feb 2 '06 #9
Jeff Flinn <NO****@nowhere.com> wrote:
Marcus Kwok wrote:
if (mySet.find(myNumber) != mySet.end()) {


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.

--
Marcus Kwok
Feb 2 '06 #10
REH

bu*****@gmail.com 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.


You are welcome to use mine:

http://www.richherrick.com/software/...k_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;

Feb 2 '06 #11

Marcus Kwok schrieb:
Ben Radford <be**************@new.ox.ac.uk> wrote:
bu*****@gmail.com 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;
}

--
Marcus Kwok


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

--

Henryk

Feb 3 '06 #12
On 2006-02-03 10:15:08 -0500, "Henryk" <he************@gmx.de> said:

Marcus Kwok schrieb:
Ben Radford <be**************@new.ox.ac.uk> wrote:
bu*****@gmail.com 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;
}

--
Marcus Kwok


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;
}
--
Clark S. Cox, III
cl*******@gmail.com

Feb 3 '06 #13
REH

Henryk wrote:
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

--

Henryk


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

Feb 3 '06 #14

REH schrieb:
Henryk wrote:
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

--

Henryk


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.

--

Henryk

Feb 3 '06 #15
REH

"Henryk" <he************@gmx.de> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...

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
Feb 3 '06 #16

Henryk wrote:
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

Feb 3 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
4
by: GianGuz | last post by:
Global new and delete operators can be overloaded to suite particulars needs. Typically they are overloaded to insert useful debugging/trace informations. What I would to discuss here concerns the...
6
by: bearophileHUGS | last post by:
Sometimes I suggest to add things to the language (like adding some set methods to dicts), but I've seen that I tend to forget the meaning of six set/frozenset operators: s & t s &= t s | t s...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
49
by: raju | last post by:
hi can we compare two integers without using relational operators (== != < <= > >=) thanks rajesh s
0
by: Syanide | last post by:
here's a bit info for you fellas: eg.. you have Square classes u wanna add public static int operator+ (Square s1, Square s2) { // your code here } comparision operators..
17
by: Steve R. Hastings | last post by:
I have been studying Python recently, and I read a comment on one web page that said something like "the people using Python for heavy math really wish they could define their own operators". The...
28
by: dspfun | last post by:
I'm trying to get a good understanding of how unary operators work and have some questions about the following test snippets. int *p; ~!&*++p--; It doesn't compile, why? The problem seems to be...
18
by: Zach | last post by:
Can someone list the various macro operators and what they mean. Came across a function macro: #define max(a, b) ((a)>(b)?(a):(b)) What does "?" amd ":" mean in this statement? Zach
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.