membership in a set in C++

S

sara

Hi All,

Is there any operator like 'in' (membership in a set) in the c++?

char x
if (x in {'a', 'b','c','d'})
//do something;


I know that it can be simulated using an array for set but I wonder it
there is more direct way to do this. Thanks.

Sara.
 
J

Jim Langston

sara said:
Hi All,

Is there any operator like 'in' (membership in a set) in the c++?

char x
if (x in {'a', 'b','c','d'})
//do something;


I know that it can be simulated using an array for set but I wonder it
there is more direct way to do this. Thanks.

Well, you can use for_each which is in <algorithm> which takes an iterator
to the first, iterator to the last and a function. It's not exactly like
you're showing, but it's close.

#include <iostream>
#include <algorithm>

void DisplayChar( char x )
{
std::cout << x << "\n";
}

int main()
{
char Foo[] = "abcd";
std::for_each( Foo, Foo+4, DisplayChar );

}
 
G

gara.matt

Is there any operator like 'in' (membership in a set) in the c++?
char x
if (x in {'a', 'b','c','d'})
//do something;
I know that it can be simulated using an array for set but I wonder it
there is more direct way to do this. Thanks.

Well, you can use for_each which is in <algorithm> which takes an iterator
to the first, iterator to the last and a function. It's not exactly like
you're showing, but it's close.

#include <iostream>
#include <algorithm>

void DisplayChar( char x )
{
std::cout << x << "\n";

}

int main()
{
char Foo[] = "abcd";
std::for_each( Foo, Foo+4, DisplayChar );

}

You can implement your own set class. I've actually implemented a set
class recently for a sodoku-solver I'm in the process of writing and
if you want the source I'd be happy to email it and tell you how it
works.

Basically there are many ways you can implement a set class, but I
chose to do a practical and efficient implementation using a hash-
table.

Also, a "in" operator is much too high-level for a programming
language as C++. You may want to try scripting languages, although I
do not know of any that come with a set class built-in. You could try
matlab, scheme or (best shot would be to try) mathematica which is not
really a programming language but something mathematicians use, I'm
sure sets are built into that.
 
J

Jim Langston

Is there any operator like 'in' (membership in a set) in the c++?
char x
if (x in {'a', 'b','c','d'})
//do something;
I know that it can be simulated using an array for set but I wonder it
there is more direct way to do this. Thanks.

Well, you can use for_each which is in <algorithm> which takes an
iterator
to the first, iterator to the last and a function. It's not exactly like
you're showing, but it's close.

#include <iostream>
#include <algorithm>

void DisplayChar( char x )
{
std::cout << x << "\n";

}

int main()
{
char Foo[] = "abcd";
std::for_each( Foo, Foo+4, DisplayChar );

}

You can implement your own set class. I've actually implemented a set
class recently for a sodoku-solver I'm in the process of writing and
if you want the source I'd be happy to email it and tell you how it
works.

Basically there are many ways you can implement a set class, but I
chose to do a practical and efficient implementation using a hash-
table.

Also, a "in" operator is much too high-level for a programming
language as C++. You may want to try scripting languages, although I
do not know of any that come with a set class built-in. You could try
matlab, scheme or (best shot would be to try) mathematica which is not
really a programming language but something mathematicians use, I'm
sure sets are built into that.

I know python supports in.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi All,

Is there any operator like 'in' (membership in a set) in the c++?

char x
if (x in {'a', 'b','c','d'})
//do something;


I know that it can be simulated using an array for set but I wonder it
there is more direct way to do this. Thanks.

Use std::set to store the data and search the set to test for membership:

#include <set>

int main()
{
std::set<char> s;
s.push_back('a');
s.push_back('a');
s.push_back('a');
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Use std::set to store the data and search the set to test for membership:

#include <set>

int main()
{
std::set<char> s;
s.push_back('a');
s.push_back('a');
s.push_back('a');

Without sending too early this time:

#include <set>
#include <iostream>

int main()
{
std::set<char> s;
s.push_back('a');
s.push_back('b');
s.push_back('c');
s.push_back('d');
s.push_back('e');

if (s.find('d') != s.end())
{
std::cout << "In set" << std::endl;
}

return 0;
}
 
J

James Kanze

Is there any operator like 'in' (membership in a set) in the c++?
char x
if (x in {'a', 'b','c','d'})
//do something;

There's no set type in the language proper, so there are no
language constructs to support it.
I know that it can be simulated using an array for set but I
wonder it there is more direct way to do this.

At present, no. I think the standards committee is considering
two extensions, a sort of language supported for_each, and some
extended forms of constant initialization, which together might
allow something like:

foreach ( char x in std::set< char > ?? 'a', 'b', 'c', 'd' ?? )
...

I'm very unfamiliar with these proposals, however (the ?? in my
example simply means that I haven't the slightest idea of the
syntax really being proposed), nor how likely they are to make
it into the next version of the standard.

And of course, once adopted by the standard, who knows how long
it will take before compilers actually implement it.
 

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

No members online now.

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,137
Latest member
IndiraMcCo

Latest Threads

Top