TechHui

Hawaiʻi's Technology Community

Does the iPhone development environment (and by extension Mac) include an equivalent to the C++ STL Queue container? Before I use this in my VS2008 environment I want to make sure I won't have too much trouble converting it to an iPhone equivalent. We have tried to keep our code very generic.

Thanks,
Ken

Views: 114

Replies to This Discussion

I'm new to this (me personally, not our company), but I don't think there is a direct equivalent. I'd probably create a simple wrapper around NSMutableArray with a pop and push method. Pat or Sherwin, feel free to correct me :-)
@interface NSMutableArray (QueueAdditions)

- (id)pop;
- (void)push:(id)obj;

@end

@implementation NSMutableArray (QueueAdditions)

- (id)pop
{
id lastObject = [[[self lastObject] retain] autorelease];
if (lastObject)
[self removeLastObject];
return lastObject;
}

- (void)push:(id)obj
{
[self addObject: obj];
}

@end

NSMutableArray is actually implemented as a 2-3 tree, so its pretty fast for using as a FIFO (queue).
Thank you. Clear as mud :-) This will do, though one could wish for something a little closer to "standard" (don't get started Dan). Another question: I'll be accessing the queue from multiple threads, in particular I will be pushing from one thread and popping from another. How does one access the queue object from different threads?

Ken
Quick answer: No.

Here's links that can help you with alternatives:

http://stackoverflow.com/questions/817469/how-do-i-make-and-use-a-q...

Mike Ash provides a a Cocoa wrapper for an STL queue here:
http://www.mikeash.com/?page=pyblog/using-evil-for-good.html

Create a category on NSMutableArray:
http://forum.soft32.com/mac/Stack-class-Cocoa-ftopict46638.html

Regarding C++ in Cocoa, see:
http://www.cocoadev.com/index.pl?CPlusPlus

HTH

RSS

Sponsors

web design, web development, localization

© 2024   Created by Daniel Leuck.   Powered by

Badges  |  Report an Issue  |  Terms of Service