By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,505 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,505 IT Pros & Developers. It's quick & easy.

remove spaces from begin a string

P: n/a
im made subj like this:

inline
bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; }

str.erase (
std.begin ()
, std::find_if ( str.begin(), str.end(), isnt_space)
);

How i can use std for better away?
Can i invert result of the ::isspace by means of bind2nd?

--
www.andr.mobi
Dec 27 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 27 ΔΕΛ, 13:59, Andrew Wingorodov <m...@andr.cawrote:
im made subj like this:

inline
bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; }

str.erase (
š š š š š std.begin ()
š š š š , std::find_if ( str.begin(), str.end(), isnt_space)
);

How i can use std for better away?
You can read manual on STL. Check something like `not1' (thats a
`one' which means `negate unary predicate').

Dec 27 '07 #2

P: n/a
Andrew Wingorodov <ma**@andr.cawrote:
im made subj like this:

inline
bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; }
The above could have been written more compactly as:

inline bool isnt_space( int sp ) { return !::isspace( sp ); }
str.erase (
std.begin ()
, std::find_if ( str.begin(), str.end(), isnt_space)
);

How i can use std for better away?
Can i invert result of the ::isspace by means of bind2nd?
Try this:

str.erase( str.begin(), find_if( str.begin(), str.end(),
not1( ptr_fun( &::isspace ) ) ) );

ptr_fun turns the function into an adaptable unary function
(http://www.sgi.com/tech/stl/AdaptableUnaryFunction.html)

not1 creates a unary_negate object, which inverts the result of the
function passed to it.
(http://www.sgi.com/tech/stl/unary_negate.html)

There may be a question as to whether what I provided is a "better way"
than what you did.

Of course you could always do both...

const unary_negate<pointer_to_unary_function<int, int
isnt_space = not1( ptr_fun( &::isspace ) );

str.erase( str.begin(),
find_if( str.begin(), str.end(), isnt_space ) );
Dec 27 '07 #3

P: n/a
Daniel T. <da******@earthlink.netwrote:
>bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; }

inline bool isnt_space( int sp ) { return !::isspace( sp ); }
OK
Try this:

str.erase( str.begin(), find_if( str.begin(), str.end(),
not1( ptr_fun( &::isspace ) ) ) );

str.erase( str.begin(),
find_if( str.begin(), str.end(), isnt_space ) );
10x

--
www.andr.mobi
Dec 28 '07 #4

P: n/a
Andrew Wingorodov wrote:
im made subj like this:

inline
bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; }

str.erase (
std.begin ()
, std::find_if ( str.begin(), str.end(), isnt_space)
);

How i can use std for better away?
Can i invert result of the ::isspace by means of bind2nd?
This is what I use to trim spaces from the beginning and end of a
std::string:

std::string trim( const std::string& text, const char TrimChar = ' ' );

std::string trim( const std::string& text, const char TrimChar )
{
std::string::size_type First = text.find_first_not_of(TrimChar);
if ( First == std::string::npos )
return "";
return text.substr( First, text.find_last_not_of(TrimChar) - First +
1);
}

This will remove the trim char (defaults to space, but you can make it
anything) from the beginning and end of a string.

--
Jim Langston
ta*******@rocketmail.com
Dec 28 '07 #5

P: n/a
"Jim Langston" <ta*******@rocketmail.comwrote:
Andrew Wingorodov wrote:
inline
bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; }

str.erase (
std.begin ()
, std::find_if ( str.begin(), str.end(), isnt_space)
);

How i can use std for better away?
Can i invert result of the ::isspace by means of bind2nd?

This is what I use to trim spaces from the beginning and end of a
std::string:

std::string trim( const std::string& text, const char TrimChar = ' ' );

std::string trim( const std::string& text, const char TrimChar )
{
std::string::size_type First = text.find_first_not_of(TrimChar);
if ( First == std::string::npos )
return "";
return text.substr( First, text.find_last_not_of(TrimChar) - First +
1);
}

This will remove the trim char (defaults to space, but you can make it
anything) from the beginning and end of a string.
Note, isspace returns true for 6 different characters...
Dec 28 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.