Hii..
I m using string vector my element set is L1 L2 L3 L4 L5 what i want now is to remove the whitespaces. so that my elements become L1L2L3L4L5.
I tried using remove but it doesn't check as it can't find a whitespace separately means when it iterate over second element it's "spaceL2" and not space so it simply ignore that same problem with find method method as well.
Kindly help

Recommended Answers

All 12 Replies

post code and an actual example string. For example: "a b c" replaced by "abc". If that's what you want to do when code a while loop something like this:

string::size_t pos;
while( (pos = mystring.find(' ')) != string::npos)
{
    mystring = mystring.substr(0,pos) + mystring.substr(pos+1);   
}

The code that you have given how should i put it back into my vector<string>

My code is as follows :-

vector<string>vertex;
for(vector<string>iterator it=vertex.begin();it!=vertex.end();it++)
cout<<(*it);

This gives an output L1 L2 L3 L4 L5
Means here the first element is L1
Second element is \tL2
Third element is \tL3
Fourth element is \tL4
Fifth element is \tL5

"\t means a whitespace"
Now what i want is to remove all the preceeding whitespaces from all the elements.
means when i access vector it should give me values like L1L2L3L4L5

Slightliy more complicated. You need to use a reference to the string in the vector. The algorithm below removes white space found anywhere in the string.

for(vector<string>::iterator it=vertex.begin();it!=vertex.end();it++)
{
    std::string& mystring = *it;
    std::size_t pos;
    while( (pos = mystring.find_first_of(" \t\r\n")) != string::npos)
    {
        if(pos > 1)
        {
            mystring = mystring.substr(0,pos) + mystring.substr(pos+1);   
        }
        else
        {
            mystring = mystring.substr(pos+1);   
        }
    }

}

Now i have to again add this to the original vector how should i do that

Now i have to again add this to the original vector how should i do that

Please re-read my previous post becasue I tested and changed it. That algorithm works.

dragon's algorithm does work. here is a version which a. uses isspace (handle all char sets) b. uses erase instead of extract substrings and concatenate (little more efficient). c. continues from the next char while repeatedly searching for whitespaces (again, little more efficient)

void remove_ws( vector<string>& vertex )
{
  int (*pfn_isspace)(int) = &isspace ;
  for( size_t i=0 ; i<vertex.size() ; ++i )
  {
      std::string& mystring = vertex[i] ;
      string::iterator where = mystring.begin() ;
      while(true)
      {
        where = find_if( where, mystring.end(), pfn_isspace ) ;
        if( where == mystring.end() ) break ;
        where = mystring.erase(where) ;
      }
  }
}

Or this to avoid break. Yes I like your suggestion. You don't need the function pointer variable.

void remove_ws( vector<string>& vertex )
{
  for( size_t i=0 ; i<vertex.size() ; ++i )
  {
      std::string& mystring = vertex[i] ;
      string::iterator where = mystring.begin() ;
      while( (where = find_if( where, mystring.end(), issspace )) != mystring.end() )
      {
        where = mystring.erase(where) ;
      }
  }
}

Here isspace is a character or a predefined constant in some header??

> Or this to avoid break.
true, looks much neater
> You don't need the function pointer variable.
g++ (even very recent versions like 4.2) still does not have fully compliant "shadow headers" and can cause problems unless we tell the compiler explicitly that we intend using a pointer to a function. http://members.aon.at/hstraub/linux/newscache/porting-howto.html#sec-macros
this is a problem with the gnu libstdc++ . the code as dragon wrote (without the function pointer variable) works with most conforming implementations (microsoft, comeacu,icc).

Here isspace is a character or a predefined constant in some header??

isspace() is a macro defined in ctype.h header file. It is also be implemented as a standard C function which is why it can be passed to another function as a function pointer. Its a platform-independent way of checking if a character is whitespace.

I m using following code now but it is giving error as "C:\SRLGrp\csv\CVSreader\csvreader.cpp(28) : error C2914: 'find_if' : cannot deduce template argument as function argument is ambiguous"

bool isspace(string ch)
{
if(ch=="")
return true;
else
return false;
}


void remove_ws( vector<string>& sample )
{


for( size_t i=0 ; i<sample.size() ; ++i )
{
std::string& mystring = sample ;
string::iterator where = mystring.begin() ;


do
{
where = find_if( where, mystring.end(),isspace);
where = mystring.erase(where) ;
}while(where!=mystring.end());
}
}

Why have you created your own isspace function? You'd be better off using the one declared in <cctype>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.