|
|
大家帮我看看。
////////////file mystring.h
#ifndef MYSTRING_H
#define MYSTRING_H
#include<string>
using namespace std;
class MyString :public string
{
public:
MyString(void):string(){};
MyString(const char* str):string(str){};
MyString(const string& basd):string(basd){};
//Returns a MyString that has whitespace removed from the start and the end
MyString stripWhiteSpace( ) const;
/*
Returns a string that has whitespace removed from the start and the end,
and which has each sequence of internal whitespace replaced with a single space.
*/
MyString simplefyWhiteSpace(void) const;
};
#endif //MYSTRING_H
////////////file mystring.cpp
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "mystring.h"
MyString MyString::stripWhiteSpace( ) const
{
string::size_type begin,end,len=length();
string strTemp = substr(0, len);
begin =0;
while(begin<=len)
{
if ( isspace(strTemp[begin]) ) begin++;
else break;
}
end = len - 1;
while(end>=0)
{
if(isspace(strTemp[end])) end--;
else break;
}
return substr(begin,end - begin+1);
}
MyString MyString::simplefyWhiteSpace() const
{
MyString strTemp = stripWhiteSpace();
string::size_type len = strTemp.length();
char *s = new char[len];
strcpy(s,strTemp.c_str());
char *p;
char *token=" \t\n\v\f";
string strret = strtok(s,token);
while ( p=strtok(NULL,token) ) strret +=p;
delete [] s;
return strret;
}
/////////////file main.cpp
#include <iostream>
using namespace std;
#include "mystring.h"
int main(void)
{
MyString ilbc = " asdfl \n lkeop; llel' ";
ilbc = ilbc.simplefyWhiteSpace();
cout << ilbc << endl;
return 0;
}
在linux下面,我用g++编译后运行能得到正确的结果..
但是在Win下面我用Vc编译能通过.但是运行时提示Debug Error.如果我把 delete [] s 这条语句注释掉,在Vc下面就能运行得到正确的结果...
why? |
|