Re: Newbie question on strings
On May 25, 11:43 pm, "Daniel T." <danie...@earthlink.net> wrote:
In article <1180107394.829626.23...@q69g2000hsb.googlegroups.com>,
Very good, but...
#include <algorithm>
#include <iterator>
#include <iostream>
using namespace std;
bool is_digit( char c )
{
return isdigit( c );
Where is isdigit defined. There are isdigit functions defined
in two different standard headers; you didn't include either of
them, and the call here is legal with none of them, it's either:
#include <locale>
// ...
return std::isdigit( c, std::locale() ) ;
or
#include <ctype.h>
// ...
return isdigit( static_cast< unsigned char >( c ) ) ;
Given the way you've written it, why write it at all?
}
int main()
{
string input;
getline( cin, input );
I'd check that the read succeeded, just to be sure.
const string::iterator it =
stable_partition( input.begin(), input.end(), &is_digit );
In theory, you could use the two argument version of
std::isdigit directly in the call here, by means of ptr_fun and
bind2nd. But since these are all templates, as is the two
argument function, you'd have to explicitly tell the compiler
which instantiation to use. The resulting expression is not
particularly succinct.
If you do this sort of thing for a living, of course, you'll
probably have the necessary predicates already present in your
library, so it would be a lot simpler.
cout << "x = ";
copy( input.begin(), it, ostream_iterator<char>( cout ) );
cout << "\ny = ";
copy( it, input.end(), ostream_iterator<char>( cout ) );
(You forgot the final newline.)
I'd probably write this:
cout << "x = " << std::string( input.begin(), it ) << '\n' ;
cout << "y = " << std::string( it, input.end() ) << '\n' ;
It's probably less efficient this way, but IMHO considerably
more readable.
}
I do like your use of a standard algorithm.
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34