Set of values

  • Thread starter Thread starter Edward Diener
  • Start date Start date
E

Edward Diener

In C++ Builder there was a construct called a Set, based on a an
enumeration, where more than one enumerated value could make up the set so
that any combination of enumerated values in an enum could be part of the
Set. It was implemented as a template, and was compatible with the built-in
Set of Object Pascal/Delphi. As an example:

enum SomeEnum
{
Value1,
Value2,
Value3
};

Set<SomeEnum,Value1,Value3> mySet;

and then one had operators/member functions to add, remove, and check
whether a given enumerated value was in the set.

Does the .NET framework have anything like this, which I can use in my MC++
modules and components in a CLS compliant way so that other .NET languages
can interoperate with my construct ?

The closest thing I can think of is a BitArray or BitVector32 class, but the
downside to this is specifying what each bit represents. I am not sure what
is the best CLS way to do the latter. Any information on this is welcome as
I am sure other .NET developers have faced the same problem.
 
The C++ standard template library offers the container
you are looking for.

Try:

#include <set>

enum SomeEnum { Value1, Value2, Value3 };

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

mySet.insert( Value2 );
// Insert an element into the set.

if ( mySet.find( Value3 ) != mySet.end() ) {
// check if Value3 is in the set
 
Sorry for the interrupted message before (too many
fingers on keyboard error). Here is the completed reply...

The C++ standard template library offers the container
you are looking for.

Try:

#include <iostream>
#include <set>

enum SomeEnum { Value1, Value2, Value3, Value4 };

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

std::cout << "mySet now contains " << mySet.size()
<< " elements.\n";

mySet.insert( Value1 );
mySet.insert( Value2 );
mySet.insert( Value3 );
// Insert some elements into the set.

std::cout << "mySet now contains " << mySet.size()
<< " elements.\n";

mySet.erase( Value3 );
// erase one of the elements.

std::cout << "mySet now contains " << mySet.size()
<< " elements.\n";

// check if some value is member of set.
if ( mySet.find( Value2 ) != mySet.end() ) {
std::cout << "Value2 is in set\n";
}
else {
std::cout << "Value2 is not in set\n";
}

// loop through all current members of the set
for ( std::set<enum SomeEnum>::const_iterator p =
mySet.begin(); p != mySet.end(); ++p )
{
std::cout << "Member: " << *p << "\n";
}

return 0;
}
 
Michael said:
The C++ standard template library offers the container
you are looking for.

Try:

#include <set>

enum SomeEnum { Value1, Value2, Value3 };

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

mySet.insert( Value2 );
// Insert an element into the set.

if ( mySet.find( Value3 ) != mySet.end() ) {
// check if Value3 is in the set

Quite true, but it is clearly not CLS compliant unless I wrap it with a __gc
class, which is quite possible. Thanks !
 
Back
Top