Re: counting characters
Re: counting characters
- Subject: Re: counting characters
- From: Allan Odgaard <email@hidden>
- Date: Thu, 15 Apr 2004 19:50:49 +0200
On 15. Apr 2004, at 17:52, Jonathan E. Jackel wrote:
The most compact code for counting characters that I can come up with
is:
Compact? Then how about:
NSString* str = @"Hello world";
// count number of o's in string
occurrences = std::count(beginof(str), endof(str), 'o');
// count number of letters in string
occurrences = std::count_if(beginof(str), endof(str), is_letter);
Where is_letter could be:
bool is_letter (unichar ch)
{
static NSCharacterSet* letters = [NSCharacterSet
letterCharacterSet];
return [letters characterIsMember:ch];
}
Or it could be made using std::bind and unary_method :) of cause the
above is using CocoaSTL.
** Cocoa FAQ: <
http://www.alastairs-place.net/cocoa/faq.txt> **
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.