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.