Re: Word count
Re: Word count
- Subject: Re: Word count
- From: Allan Odgaard <email@hidden>
- Date: Thu, 10 Jun 2004 06:31:31 +0200
On 10. Jun 2004, at 5:26, Matt Baker wrote:
So, for anyone who's run into counting / parsing, what's the quickest
way to accurately count words with NSScanner (or anything else in
Cocoa)?
Well, here's a generic C++ function to count words:
template <typename InputIter, typename Predicate>
size_t word_count (InputIter first, InputIter last, Predicate word_char)
{
size_t res = 0;
while(first != last)
{
InputIter bow = std::find_if(first, last, word_char);
InputIter eow = std::find_if(bow, last, std::not1(word_char));
if(bow != eow)
++res;
first = eow;
}
return res;
}
Using CocoaSTL it can be used on an NSString like this:
NSString* str = @"Hello, world!";
size_t cnt = word_count(beginof(str), endof(str),
std::bind1st(unary_method<BOOL, unichar>("characterIsMember:"),
[NSCharacterSet letterCharacterSet]));
The third argument is just a fancy way to construct a function on the
fly to return true when handed a unicode character in the letter
character set :)
_______________________________________________
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.