我想在Objective-C程序中使用队列数据 struct .在C++中,我使用STL队列.Objective-C中的等效数据 struct 是什么?如何推送/弹出项目?
我想在Objective-C程序中使用队列数据 struct .在C++中,我使用STL队列.Objective-C中的等效数据 struct 是什么?如何推送/弹出项目?
Ben的版本是一个堆栈而不是队列,所以我对它进行了一些调整:
NSMutableArray+QueueAdditions.h
@interface NSMutableArray (QueueAdditions)
- (id) dequeue;
- (void) enqueue:(id)obj;
@end
NSMutableArray+QueueAdditions.m
@implementation NSMutableArray (QueueAdditions)
// Queues are first-in-first-out, so we remove objects from the head
- (id) dequeue {
// if ([self count] == 0) return nil; // to avoid raising exception (Quinn)
id headObject = [self objectAtIndex:0];
if (headObject != nil) {
[[headObject retain] autorelease]; // so it isn't dealloc'ed on remove
[self removeObjectAtIndex:0];
}
return headObject;
}
// Add to the tail of the queue (no one likes it when people cut in line!)
- (void) enqueue:(id)anObject {
[self addObject:anObject];
//this method automatically adds to the end of the array
}
@end
只需导入.h文件,并像调用其他NSMutableArray方法一样调用它们.
祝你好运,继续编码!