私はインデックス[1 ... 20]の配列を持っています。 indicesArrayの最初の4つの要素は、特定の型のファイル(型Aと呼ばれます)にリンクされ、他の16個はB型にリンクされます。列挙型を使用して整数の範囲に型を割り当てる?
ランダムに配列をシャッフルします。今では4つのインデックスを抽出したいと思いますが、最大でも4つのタイプのうち1つだけがタイプAであることができます。
ここでenum関数を使用してインデックス1〜4を "タイプA"として定義する必要があると思います&インデックス5-20を「タイプB」とし、もし私が例えば私の新しくランダム化されたindicesArray [0]の最初の要素は、どのタイプがそれに応じて&であるかを知ることができました。
私はそれのようなものだ列挙型の例から使用さ見てきた道:
enum category { typeA = 0, typeB };
はTYPEBにタイプA &にインデックス1-4に休息を割り当てることが可能です、または私は間違ってトラックにしていますここに?前もって感謝します。
編集は、私はこの&をすぐにエラーに
私はエラーを取得し、これをコンパイルしよう
#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 to re-enable "は、通常、第2のメインが偶然、またはそのような何かにミックスされたときに発生します。何か案は?
編集練習
あなたがトラックオフにしている#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;
}
は包括 'N-1 'に' 0 'から行くCにインデックスを注意してください。 – pmg
ありがとうpmg :)はい私が参照する配列はインデックスを保持しています(1から始まる)。 – Octave1