程序代码:
#include <iostream>
#include <cctype>
#include <algorithm>
#include <functional>
using namespace std;
int main( void )
{
const string s = "asjjk 12rtr 345 dasdd9&k32*dfasfkjk##dasfdf897";
for( auto itor=cbegin(s); itor!=cend(s); )
{
bool is_digit = isdigit(*itor);
bool is_alpha = isalpha(*itor);
if( is_digit || is_alpha )
{
cout << (is_digit ? "DIGIT: " : "ALPHA: ");
auto itor_end = std::find_if( itor, end(s), is_digit ? not_fn(::isdigit) : not_fn(::isalpha) );
cout.write(&*itor,distance(itor,itor_end)) << '\n';
itor = itor_end;
}
else
{
itor = std::find_if( itor, end(s), ::isalnum );
}
}
}
输出
ALPHA: asjjk
DIGIT: 12
ALPHA: rtr
DIGIT: 345
ALPHA: dasdd
DIGIT: 9
ALPHA: k
DIGIT: 32
ALPHA: dfasfkjk
ALPHA: dasfdf
DIGIT: 897
排序用 std::sort