数组比较

gonguui1982 2008-12-16 02:07:09
两个数组如何比较是否相同,忽略元素的顺序.
例如{a,b} 和{b,a} 这2个数组怎么比能比较出他们是相同的.有现成的方法吗?
...全文
256 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
非凡之臂 2009-02-10
  • 打赏
  • 举报
回复
int memcmp(
const void* buf1,
const void* buf2,
size_t count
);

inline int wmemcmp(
const wchar_t* buf1,
const wchar_t* buf2,
size_t count
);

#include <string.h>
#include <stdio.h>

void main( void )
{
char first[] = "12345678901234567890";
char second[] = "12345678901234567891";
int result;

printf( "Compare '%.19s' to '%.19s':\n", first, second );
result = memcmp( first, second, 19 );
if( result < 0 )
printf( "First is less than second.\n" );
else if( result == 0 )
printf( "First is equal to second.\n" );
else if( result > 0 )
printf( "First is greater than second.\n" );
printf( "Compare '%.20s' to '%.20s':\n", first, second );
result = memcmp( first, second, 20 );
if( result < 0 )
printf( "First is less than second.\n" );
else if( result == 0 )
printf( "First is equal to second.\n" );
else if( result > 0 )
printf( "First is greater than second.\n" );
}
Output
Compare '1234567890123456789' to '1234567890123456789':
First is equal to second.
Compare '12345678901234567890' to '12345678901234567891':
First is less than second.
vrhero 2008-12-16
  • 打赏
  • 举报
回复
值类型数组可以用Linq...
return a.Except(b).Count==0 && b.Except(a).Count==0;

引用类型只有排序后循环比对了...
gonguui1982 2008-12-16
  • 打赏
  • 举报
回复
谢谢,我正在冒泡呢,呵呵
GTX280 2008-12-16
  • 打赏
  • 举报
回复
1楼正解
CloneCenter 2008-12-16
  • 打赏
  • 举报
回复
1楼说的没错,建议排序后比较每个元素。
wwmwenzhu 2008-12-16
  • 打赏
  • 举报
回复
没有,排序后比较每一个元素!!

111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧