STL容器相当于Delphi的设置?

Ala*_*inD 3 c++ delphi stl

是否存在功能类似于Delphi"set"的STL容器,以下代码取自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;
Run Code Online (Sandbox Code Playgroud)

Fal*_*nUA 7

与Delphi最接近的set of是标题中的STL std::bitset容器<bitset>.

相似点:

  • 您可以在开始时设置其范围;
  • std::bitset::setInclude()在Delphi中等于;
  • std::bitset::resetExclude()在Delphi中等于;
  • std::bitset::test()in在Delphi中等于;
  • 您可以使用按位运算符(|,&,<<,>>,^,等).

区别:

  • 在Delphi中,a的最大大小set是256位,所以如果你想创建一个更大的集合,你必须使用类似的东西array [1..N] of set of byte.在C++中,std::bitset没有那个限制;
  • 在Delphi中,set按值包含/排除位.在C++中,std::bitset通过索引设置/重置位.