char []的无故障交换 [英] no-fail swap for char []'s

查看:66
本文介绍了char []的无故障交换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


1)假设你有两个相同大小的char []'。你会如何为他们写一个

无故障交换方法?例如:


class Test

{

char s [10];

无效交换(Test& rhs)

{

//是否有可能将数据成员的''与'无法交换

保证?

}

};

2)如果数据成员是指针(char *),则std :: swap足够(如果另一个指针被分配了不同的大小,则甚至

)?例如:


class Test

{

char * s; //假设这被分配到某个大小

void swap(Test& rhs)

{

std :: swap(s,rhs) .s); //这可以吗?

}

};


谢谢,


- Dennis

解决方案



" Dennis Jones" < no **** @ nospam.comwrote in message

news:6Jxjj.17776


Y63.13879@trnddc03 ...
< blockquote class =post_quotes>
大家好,


1)假设你有两个相同大小的char []'。你会如何为他们写一个无故障交换方法?
?例如:


class Test

{

char s [10];

无效交换(Test& rhs)

{

//是否有可能将数据成员的''与'无法交换

保证?

}

};



好​​了,在考虑了几分钟后,我想出了这个:


void strswap(char * const c1,char * const c2)

{

int size = std :: max(strlen(c1),strlen(c2))+ 1;

for(int c = 0; c< size; ++ c)

{

std :: swap(c1 [c],c2 [c] );

}

}


等级测试

{

char s [10]; //保证是一个有效的字符串

void swap(Test& rhs)

{

strswap(s,rhs.s);

}

};


这样合理吗?


- 丹尼斯




" Alf P. Steinbach" < al *** @ start.nowrote in message

news:13 ************* @ corp.supernews.com ...
< blockquote class =post_quotes>


>好吧,在考虑了几分钟之后,我想出了这个:

void strswap(char * const c1,char * const c2)
{size = std :: max(strlen(c1),strlen(c2))+ 1;
for(int c = 0; c< size; ++ c)
{
std :: swap(c1 [c],c2 [c]);
}
}


你提出的问题看起来像是家庭作业。



你好Alf,我希望你或Victor能回应!


不,我是专业人士,虽然我做在家工作,所以我猜你*可以*

称之为功课!但这不是功课。我提供的示例代码是

,只是为了演示我想在现有应用程序中做什么来提高现有代码和strswap()函数的可靠性
其中
(这太糟糕了):


void strswap(char * const c1,char * const c2)

{

char * temp = strdup(c1);

strcpy(c1,c2);

strcpy(c2,temp);

free(temp);

}


但是既然你现在已经付出了努力:


没有指定数组包含nullterminated字符串,



我添加了//保证是一个有效的字符串评论表明

他们是空终止的(我知道,我在第一篇文章中没有这么说)。

类都有构造函数,可以保证成员要么初始化为零,要么初始化为有效字符串。


因此std :: strlen是禁忌的。



我理解你的观点,但他们*将*以空值终止。


规定数组的大小相同,因此std :: max与

禁忌。



我也明白你的观点。使用strlen和max的目的是

来确定我可以交换的最小字符数,而不会丢失任何

数据。交换阵列的全部内容是没有意义的,如果他们不需要



现在,如果我去的话明确地执行交换成员函数中的工作,然后我

*可以*做:


class测试

{

char s [10];

void swap(Test& rhs)

{

for(int i = 0; i< sizeof(s)/ sizeof(s [0]); ++ i)

{

std :: swap(s [i],rhs。 s [i]);

}

}

};


确实避免了额外的strlen和max调用(这可能会影响函数的效率)。但是,我宁愿不必在包含

许多char []数据成员的许多不同类中一遍又一遍地重写相同的东西(DRY原则)这就是为什么我写了

独立的strswap()函数[所以我不必为每个char []数据反复编写相同的代码

每个班级的成员 - 那将是

荒谬!!]。


因此,对于可重复使用的东西,我选择了一个独立的功能。要使
足够通用,可以将它与所有这些

类中的任何char []数据成员一起使用,就必须根据数据本身进行循环。虽然,我想b $ b假设我也可以这样做:


void strswap(char * const c1,char * const c2,size_t length)

{

for(int i = 0; i< length; ++ i)

{

std :: swap(c1 [i],c2 [i]);

}

}


等级测试

{

char s [C]; //其中C是常量

无效交换(测试& rhs)

{

strswap(s,rhs.s,sizeof( s)/ sizeof(s [0]));

}

};


对于提供可交换字符串的实践,请使用std :: string。



我使用std :: string(实际上是它的一个变种)和更新的代码,但我不会这样做b $ b选项在这里(不是没有很多重写),因为这是非常好的遗留代码。


所有的例子做同样的基本事情,那就是交换两个char数组的

内容。我的问题是不是strlen或max

是否反对,就好像它是一个家庭作业。我的问题是,

如何在交换''sometype''数组时提供无故障保证'

(我们可能也在谈论一个数组任何类型的)?


所以,更具体的说,循环是一种令人满意的交换数组元素的方式

仍然提供无故障保证?我无法想到其他任何方式 -

你可以吗?


- 丹尼斯


Hi all,

1) Let''s say you have two char []''s of the same size. How would you write a
no-fail swap method for them? For example:

class Test
{
char s[10];
void swap( Test &rhs )
{
// is it possible to swap the data member ''s'' with the no-fail
guarantee?
}
};
2) If the data member is a pointer (char *), is std::swap sufficient (even
if the other pointer was allocated with a different size)? For example:

class Test
{
char *s; // assume this is allocated to some size
void swap( Test &rhs )
{
std::swap( s, rhs.s ); // is this okay?
}
};

Thanks,

- Dennis

解决方案


"Dennis Jones" <no****@nospam.comwrote in message
news:6Jxjj.17776


Y63.13879@trnddc03...

Hi all,

1) Let''s say you have two char []''s of the same size. How would you write
a no-fail swap method for them? For example:

class Test
{
char s[10];
void swap( Test &rhs )
{
// is it possible to swap the data member ''s'' with the no-fail
guarantee?
}
};

Well, after thinking about it for a few minutes, I came up with this:

void strswap( char * const c1, char * const c2 )
{
int size = std::max( strlen(c1), strlen(c2) ) + 1;
for ( int c=0; c<size; ++c )
{
std::swap( c1[c], c2[c] );
}
}

class Test
{
char s[10]; // guaranteed to be a valid string
void swap( Test &rhs )
{
strswap( s, rhs.s );
}
};

Is this reasonable?

- Dennis



"Alf P. Steinbach" <al***@start.nowrote in message
news:13*************@corp.supernews.com...

>Well, after thinking about it for a few minutes, I came up with this:

void strswap( char * const c1, char * const c2 )
{
int size = std::max( strlen(c1), strlen(c2) ) + 1;
for ( int c=0; c<size; ++c )
{
std::swap( c1[c], c2[c] );
}
}


The questions you posed looked like homework.

Hi Alf, I was hoping you or Victor would respond!

No. I am a professional, though I do work from home, so I guess you *could*
call it homework! But it is NOT schoolwork. The example code I provided is
just to demonstrate what I would like to do in an existing application to
improve the reliability of the existing code and the strswap() function
therein (which is awful):

void strswap( char * const c1, char * const c2 )
{
char *temp = strdup( c1 );
strcpy( c1, c2 );
strcpy( c2, temp );
free( temp );
}

But since you now have made an effort:

It was not specified that the arrays contained nullterminated strings,

I added the "// guaranteed to be a valid string" comment to indicate that
they are null-terminated (I know, I didn''t say so in my first post). The
classes all have constructors which guarantee that the members are either
initialized to zero, or otherwise initialized to a valid string.

hence std::strlen is contra-indicated.

I understand your point, but they *will* be null-terminated.

It was specified that the arrays were of the same size, hence std::max is
contra-indicated.

I understand your point here too. The purpose of using strlen and max was
to determine the minimum number of characters I could swap and not lose any
data. There''s no point in swapping the entire content of the arrays if they
don''t need to be.

Now, if I were to explicitly do the work in the swap member function, then I
*could* do:

class Test
{
char s[10];
void swap( Test &rhs )
{
for ( int i=0; i<sizeof(s)/sizeof(s[0]); ++i )
{
std::swap( s[i], rhs.s[i] );
}
}
};

That does avoid the extra strlen and max calls (which might affect the
efficiency of the function). However, I would prefer not to have to
re-write the same thing over and over in many different classes that contain
many char[] data members (DRY principle), which is why I wrote the
standalone strswap() function [so I wouldn''t have to write the same code
over and over for every char[] data member in every class -- that would be
ridiculous!!].

So, for something that is re-usable, I opted for a standalone function. To
be generic enough to use it with any of the char[] data members in all those
classes, it was necessary to loop based on the data itself. Though, I
suppose I could also do:

void strswap( char * const c1, char * const c2, size_t length )
{
for ( int i=0; i<length; ++i )
{
std::swap( c1[i], c2[i] );
}
}

class Test
{
char s[C]; // where C is some constant
void swap( Test &rhs )
{
strswap( s, rhs.s, sizeof(s)/sizeof(s[0]) );
}
};

For the in-practice of providing swappable strings, use std::string.

I do use std::string (actually a variant of it) with newer code, but I don''t
have that option here (not without a LOT of re-writing) because this is very
old, legacy code.

All of the examples do the same basic thing, and that is to swap the
contents of two char arrays. My question isn''t whether or not strlen or max
is contra-indicated, as if it were a homework assignment. My question is,
how do I provide the no-fail guarantee when swapping an array of ''sometype''
(we might just as well be talking about an array of any type)?

So, to be more specific, is a loop a satisfactory way to swap array elements
and still provide the no-fail guarantee? I can''t think of any other way --
can you?

- Dennis


这篇关于char []的无故障交换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆