【问题标题】:Assigning types to a range of integers using enum?使用枚举将类型分配给一系列整数?
【发布时间】:2012-01-31 20:16:32
【问题描述】:

我有一个索引数组 [1 ... 20]。 indicesArray 的前 4 个元素链接到某个类型的文件(称为 A 类),其他 16 个元素链接到 B 类。

我随机打乱数组。我现在希望提取 4 个索引,但最多 4 个索引中只有一个可以是 A 类型。

我想我需要在这里使用枚举函数将索引 1-4 定义为“类型 A”,将索引 5-20 定义为“类型 B”,然后如果我查看例如我新随机化的 indicesArray[0] 的第一个元素我可以判断它是哪种类型并采取相应的行动。

我从示例中看到枚举使用的方式类似于:

enum category { typeA = 0, typeB };

是否可以将索引 1-4 分配给 typeA 并将其余的分配给 typeB,还是我在这里走错了路?提前致谢。

编辑以包含代码 sn-p

我尝试对此进行测试并立即遇到错误

 #import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int* indices = malloc(20*sizeof(int));

for (int i=0; i<20; i++) {

    indices[i] = i;

}

enum category {typeA, typeB};


enum category categoryForIndex(int index) {
    if (index >= 1 && index <= 4) {
        return typeA;
    } else {
        return typeB;
    }
}

[pool drain];
return 0;

}

当我尝试编译它时,我收到错误“嵌套函数被禁用,请使用 -fnested-functions 重新启用”,这通常发生在第二个主函数意外加入混音或类似情况时。有任何想法吗?

编辑以包含一些显示如何将解决方案付诸实践的代码

#import <Foundation/Foundation.h>

enum category {typeA, typeB};

enum category categoryForIndex(int index) {
if (index >= 1 && index <= 4) {
    return typeA;
   } else {
    return typeB;
    }

}

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int* indices = malloc(20*sizeof(int));

for (int i=1; i<=20; i++) {

    indices[i] = i;

}


NSLog(@"index[0] is %i:", indices[16]);

enum category index;

index = indices[16];

switch (categoryForIndex(index)) {   //this tests to see what category 16 belongs to
    case typeA:
        NSLog(@"index is of type A");   
        break;
    case typeB:
        NSLog(@"index is of type B");
        break;
    default:
        NSLog(@"index not valid");
        break;
}

 [pool drain];
 return 0;

}

【问题讨论】:

  • 注意 C 中的索引从 0N-1,包括在内。
  • 感谢 pmg :) 是的,我指的数组包含索引(恰好从 1 开始)

标签: objective-c c enums


【解决方案1】:

你跑偏了。您不能为给定的枚举分配 1-4。枚举常量只有一个值,而且只有一个。您可以做的是使用枚举来定义两种类型,例如 typeAtypeB,就像您已经完成的那样,然后定义一个将索引映射回类型的函数,例如

enum category categoryForIndex(int index) {
    if (index >= 1 && index <= 4) {
        return typeA;
    } else {
        return typeB;
    }
}

现在您可以对索引进行分类了。

【讨论】:

  • 感谢凯文,这很有道理,我试了一下,得到了一个我没想到的错误 - 与嵌套函数有关。我在上面贴了一个小代码sn-p。
  • @Octave1:那是因为您尝试在程序的main 函数 定义一个函数。你需要把它移到外面(比如main函数之前)。
  • 很好,效果很好,我发布了一些代码,显示如何检查给定索引属于哪个类别,因为我花了一点时间才弄清楚,并且可能对某人有用.再次感谢 Dreamlax 和 Kevin。
【解决方案2】:

您可以在不先洗牌的情况下做到这一点,这样您就知道 A 总是在前面:

#define IndexCount 20
#define ExtractCount 4
#define TypeACount 4

int indicesRemainingCount = IndexCount;
int indices[IndexCount] = { ... }; // your indices, first 4 are type A
int chosenIndices[ExtractCount]; // to be filled with random elements from indices, max one of type A

int minIndex = 0;
for (int i = 0; i < ExtractCount; ++i) {
    int j = minIndex + arc4random_uniform(indicesRemainingCount - minIndex);
    chosenIndices[i] = indices[j];
    if (j < TypeACount) {
        // Set minIndex so I won't pick another A.
        minIndex = TypeACount;
    } else {
        // Remove the chosen B so I don't pick it again.
        --indicesRemainingCount;
        indices[j] = indices[indicesRemainingCount];
    }
}

【讨论】:

  • 这是合理的,只要你愿意破坏性地修改输入数组。
  • 他说他想洗牌。所以他要么修改它,要么复制它。
  • “破坏性地”我的意思是你正在丢弃有关数组中的内容的数据。不是交换单元格,而是覆盖它们。当然,如果您确实将其更改为交换,那么它就等同于 shuffle 情况。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2020-10-08
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 2014-09-20
相关资源
最近更新 更多