remove spaces from begin a string

A

Andrew Wingorodov

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?
 
P

Pavel Shved

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').
 
D

Daniel T.

Andrew Wingorodov said:
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 ) );
 
A

Andrew Wingorodov

Daniel T. said:
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
 
J

Jim Langston

Andrew said:
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.
 
D

Daniel T.

Jim Langston said:
Andrew Wingorodov wrote:

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top