AlainD
AlainD

Reputation: 6587

STL container equivalent of Delphi set?

Is there an STL container with functionality similar to the Delphi "set", the code below taken from DelphiBasics:

type
    TDigits = set of '1'..'9';       // Set of numeric digit characters
var
    digits : TDigits;                // Set variable
    myChar : char;
begin
    digits := ['2', '4'..'7'];

    // Now we can test to see what we have set on:
    for myChar := '1' to '9' do
        begin
        if (myChar In digits) then
            DoSomething()
        else
            DoSomethingElse();
        end;
end;

Upvotes: 3

Views: 723

Answers (1)

Chan Kha Vu
Chan Kha Vu

Reputation: 10390

The closest equivalent to Delphi's set of is the STL std::bitset container in the <bitset> header.

Similarities:

  • You can set its range in the beginning;
  • std::bitset::set is equal to Include() in Delphi;
  • std::bitset::reset is equal to Exclude() in Delphi;
  • std::bitset::test() is equal to in in Delphi;
  • You can use bitwise operators (|, &, <<, >>, ^, etc).

Differences:

  • In Delphi, the maximum size of a set is 256 bits, so if you want to create a larger set you have to use something like array [1..N] of set of byte. In C++, std::bitset does not have that limitation;
  • In Delphi, set bits are included/excluded by value. In C++, std::bitset bits are set/reset by index.

Upvotes: 7

Related Questions