Hmm, that was a good question
olredixsis.
An important concept in CS is that of
filters and
predicates.
http://en.wikipedia.org/wiki/Filter_%28higher-order_function%29
http://en.wikipedia.org/wiki/Predicate_%28computer_programming%29
The only filter in the STL is the
remove_copy_if() function, which gives you an extra something to wrap your brain around because it works with negative conditions instead of a straight-forward condition.
I have already given an example of a filter on lines 37-43 of my second post (
http://www.cplusplus.com/forum/beginner/17935/#msg91134 ). Here it is:
1 2 3 4 5 6 7 8 9
|
// We are only interested in alphanumeric characters, so we'll populate our
// counts using a copy of the user's input string where all unwanted
// characters have been removed.
remove_copy_if(
s.begin(),
s.end(),
back_inserter( t ),
not1( ptr_fun <int, int> ( isalnum ) )
);
|
Basically what that says is: for each character in
s (lines 5-6) that does
not satisfy the predicate "i am not an alphanumeric number" (line 8), append it to
t (line 7).
Take a look at the
remove_copy_if() docs for another description of what it does.
http://www.cplusplus.com/reference/algorithm/remove_copy_if/
Back to the idea of filtering out characters that
have repeats (leaving us with only those characters that are non-repeating), we can replace my filter example with the following:
1 2 3 4 5 6 7 8 9
|
// Now find ALL characters in the original string whose count == 1
string originals;
remove_copy_if(
t.begin(),
t.end(),
back_inserter( originals ),
not1( bind1st( mem_fun( &count_t::match1 ), &counts ) )
);
cout << "The non-repeating alphanumeric characters are: \"" << originals << "\".\n";
|
This has the convenient feature that the algorithm remains O(n) (because of my
count_t class).
Well, that's all from me for now...
Hope this helps.