Reputation: 6587
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
Reputation: 10390
The closest equivalent to Delphi's set of
is the STL std::bitset
container in the <bitset>
header.
Similarities:
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;|
, &
, <<
, >>
, ^
, etc).Differences:
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;set
bits are included/excluded by value. In C++, std::bitset
bits are set/reset by index.Upvotes: 7