本文介绍了修剪char *的最佳方式(对吗?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我写了一个修剪char *的函数,但我被告知我的方式

可能是危险的,我应该改为使用memmove(...)。

但我不确定为什么我的代码可能危险,甚至为什么可能会出现问题。

这里是代码


////////

const char * TrimLeft(char * dest)

{

if(!dest)return dest; //全部完成

size_t size = 0;

//修剪左边

while(size> = 0&&(_istspace (dest [size])||

dest [size] == 10 ||

dest [size] == 13))

{

for(size_t loop = 0; loop< strlen(dest)-1; loop ++)

dest [loop] = dest [loop +1];

dest [strlen(dest)-1] =''\ 0'';

}

返回dest;

}


const char * TrimRight(char * dest)

{

if(!dest)return dest; //全部完成

int size = int(strlen(dest));


//修剪右边

size-- ;

while(size> = 0&&(_istspace(dest [size])||

dest [size] == 10 ||

dest [size] == 13))

{

dest [size] =''\ 0'';

size--;

}

返回dest;

}


const char *修剪(char * dest)

{

TrimLeft(dest);

TrimRight(dest);

返回dest;

}


/////////////////////

//一些测试

/////////////////////

int main(int argc,char ** argv)

{

char a [10 + 1];

strcpy(a," 12345678");


char * b =新字符[10 + 1];

strcpy(b," 12345678");


修剪(a);

修剪(b);

//清洁

删除[] b;


...

返回1;

}


/////////

我想这不可能,但我会问以防万一,会

有没有办法修剪分配的内存?

我的意思是如果我这样做

char * a = new char [1024];

strcpy(a,"一个 );

修剪(a);


将''a''仍然分配1024个字符或者它可能是

''修剪''到2并释放剩余的内存?

非常感谢


Simon

Hi,

I have written a function to trim char *, but I have been told that my way
could be dangerous and that I should use memmove(...) instead.
but I am not sure why my code could be ''dangerous'' or even why there could
be a problem.

here is the code

////////
const char* TrimLeft( char *dest)
{
if (!dest ) return dest; //all done
size_t size = 0;
// trim left
while( size >= 0 && ( _istspace( dest[ size]) ||
dest[ size] == 10 ||
dest[ size] == 13))
{
for ( size_t loop = 0; loop < strlen( dest ) -1; loop++ )
dest[ loop] = dest[ loop +1];
dest[ strlen( dest ) -1 ] = ''\0'';
}
return dest;
}

const char* TrimRight( char *dest)
{
if (!dest ) return dest; //all done
int size = int(strlen( dest ));

// trim right
size--;
while( size >= 0 && ( _istspace( dest[ size]) ||
dest[ size] == 10 ||
dest[ size] == 13))
{
dest[ size] = ''\0'';
size--;
}
return dest;
}

const char* Trim( char *dest)
{
TrimLeft ( dest );
TrimRight ( dest );
return dest;
}

/////////////////////
// some test
/////////////////////
int main( int argc, char **argv )
{
char a[10+1];
strcpy( a, " 12345678 " );

char * b = new char[10+1];
strcpy( b, " 12345678 " );

Trim(a);
Trim(b);
// clean
delete [] b;

...
return 1;
}

/////////
Also I guess it is not really possible but I''ll ask just in case, would
there be a way of trimming the memory allocated?
by that I mean if I do
char *a = new char[1024];
strcpy( a, " a " );
Trim( a);

would ''a'' still have 1024 characters allocated to it or could it be
''trimmed'' to 2 and free the rest of the memory?
Many thanks

Simon

推荐答案






使用std :: string类,更简单,更安全,更高效,更实用

C ++。目前您正在编写C而不是C ++。请阅读一本关于C ++的书,该书解释了关于标准C ++库的
,例如: Josuttis的C ++标准库。


john



Use the std::string class instead, easier, safer, more efficient and real
C++. At the moment you are coding C not C++.Get a book on C++ that explains
about the standard C++ library, e.g. ''The C++ Standard Library'' by Josuttis.

john





非常低效。你有一个O(n ^ 3)算法(而* for * strlen)对于

应该只是一个线性的。

怎么样


void trim_left(char * s)

{

size_t sz = strlen(s);

char * p = find(s,s + sz,not1(isspace)));

if(p!= s&& p!= s + sz)

memmove(s,p,sz - (p - s)+ 1);

}


或者如果你要去要返回指针,只需返回p而不是

移动字符。



Very inefficient. You have an O(n^3) algorithm (while * for * strlen) for
what should only be a linear one.
how about:

void trim_left(char* s)
{
size_t sz = strlen(s);
char* p = find(s, s + sz, not1(isspace)));
if (p != s && p != s + sz)
memmove(s, p, sz - (p - s) + 1);
}

or if you are just going to return the pointer, just return p instead of
moving the chars.


这篇关于修剪char *的最佳方式(对吗?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 03:33