Is there a better way to compare two int array in C++.
有更好的方法比较c++中的两个int数组吗?
struct integer
{
int *number;
int size;
};
This is my struct and i have used this comparing function. I found this solution but it looks like bulshit.
这是我的结构,我用了这个比较函数。我找到了这个解,但它看起来像扯淡。
int BigInteger::biggest(integer number1, integer number2)
{
int biggest = 0;
if (number1.size > number2.size)
{
biggest = 1;
}
else if (number1.size < number2.size)
{
biggest = -1;
}
else
{
for (int i = number1.size; i >=0 ; --i)
{
if (number1.number[i] > number2.number[i])
{
biggest = 1;
return biggest;
}
else if (number1.number[i] < number2.number[i])
{
biggest = -1;
return biggest;
}
else
{
continue;
}
}
}
return biggest;
}
if function turn value as 1 then number1 greater than number2
if function turn value as 0 then number1 equal to number2
if function turn value as -1 then number2 greater than number1
如果函数的值是1,那么数字1大于数字2如果函数的值是0,那么数字1等于数字2如果函数的值是-1,那么数字2大于数字1
3 个解决方案
#1
4
This will not improve anything except, that it's much clearer
这将不会改善任何事情,除非,它更加清晰。
int BigInteger::biggest(integer number1, integer number2)
{
if (number1.size != number2.size)
return number1.size - number2.size;
for (int i = 0 ; i < number1.size; ++i) {
if (number1.number[i] == number2.number[i])
continue;
return number1.number[i] - number2.number[i];
}
return 0;
}
This function
这个函数
- Returns > 0 then
number1 > number2
- 返回>,然后是1 > 2
- Returns < 0 then
number1 < number2
- 返回< 0,然后返回number1 < number2
- And 0 if they are equal
- 如果它们是相等的。
Also, in c++ you can overload operators so implementing > and < should be easy using this code.
此外,在c++中,可以重载操作符,因此使用此代码实现>和 <应该很容易。< p>
#2
0
There are a few small bugs. You don't need some of this code. It will run slightly faster when it is not part of a class. And with just a bit of modification it will work with qsort()
(the parameters need to be pointers) (edit: void pointers)
有一些小bug。你不需要这些代码。当它不是类的一部分时,它会运行得稍微快一点。只要稍加修改就可以使用qsort()(参数必须是指针)(编辑:void指针)
struct integer
{
int *number;
int size;
};
int biggest(const integer &number1, const integer &number2)
{
int biggest = 0;
if (number1.size > number2.size)
{
biggest = 1;
}
else if (number1.size < number2.size)
{
biggest = -1;
}
else
{
for (int i = number1.size - 1; i >= 0; i--) // or --i, no difference
{
if (number1.number[i] > number2.number[i])
{
biggest = 1;
return biggest;
}
if (number1.number[i] < number2.number[i])
{
biggest = -1;
return biggest;
}
// don't need a continue statement here
}
}
return biggest;
}
#3
0
You can use the std::mismatch()
function like this:
您可以使用std:失配()函数如下:
int BigInteger::biggest(integer a, integer b)
{
if (a.size != b.size)
return a.size - a.size;
const auto res = std::mismatch(a.number, a.number + a.size, b.number);
if (res.first == (a.number + a.size))
return 0;
else
return (*res.first) < (*res.second) ? -1 : 1;
}
The name of the function biggest()
is quite misleading. compare()
would be much better. Also, if the function's return type would be boolean (true: a < b
), then you could use std::lexicographical_compare()
, and the code would much more simpler.
最大()函数的名称很容易引起误解。compare()会更好。此外,如果函数的返回类型是布尔型(true: a < b),那么可以使用std:: lexicographicoleical_compare(),代码会简单得多。
#1
4
This will not improve anything except, that it's much clearer
这将不会改善任何事情,除非,它更加清晰。
int BigInteger::biggest(integer number1, integer number2)
{
if (number1.size != number2.size)
return number1.size - number2.size;
for (int i = 0 ; i < number1.size; ++i) {
if (number1.number[i] == number2.number[i])
continue;
return number1.number[i] - number2.number[i];
}
return 0;
}
This function
这个函数
- Returns > 0 then
number1 > number2
- 返回>,然后是1 > 2
- Returns < 0 then
number1 < number2
- 返回< 0,然后返回number1 < number2
- And 0 if they are equal
- 如果它们是相等的。
Also, in c++ you can overload operators so implementing > and < should be easy using this code.
此外,在c++中,可以重载操作符,因此使用此代码实现>和 <应该很容易。< p>
#2
0
There are a few small bugs. You don't need some of this code. It will run slightly faster when it is not part of a class. And with just a bit of modification it will work with qsort()
(the parameters need to be pointers) (edit: void pointers)
有一些小bug。你不需要这些代码。当它不是类的一部分时,它会运行得稍微快一点。只要稍加修改就可以使用qsort()(参数必须是指针)(编辑:void指针)
struct integer
{
int *number;
int size;
};
int biggest(const integer &number1, const integer &number2)
{
int biggest = 0;
if (number1.size > number2.size)
{
biggest = 1;
}
else if (number1.size < number2.size)
{
biggest = -1;
}
else
{
for (int i = number1.size - 1; i >= 0; i--) // or --i, no difference
{
if (number1.number[i] > number2.number[i])
{
biggest = 1;
return biggest;
}
if (number1.number[i] < number2.number[i])
{
biggest = -1;
return biggest;
}
// don't need a continue statement here
}
}
return biggest;
}
#3
0
You can use the std::mismatch()
function like this:
您可以使用std:失配()函数如下:
int BigInteger::biggest(integer a, integer b)
{
if (a.size != b.size)
return a.size - a.size;
const auto res = std::mismatch(a.number, a.number + a.size, b.number);
if (res.first == (a.number + a.size))
return 0;
else
return (*res.first) < (*res.second) ? -1 : 1;
}
The name of the function biggest()
is quite misleading. compare()
would be much better. Also, if the function's return type would be boolean (true: a < b
), then you could use std::lexicographical_compare()
, and the code would much more simpler.
最大()函数的名称很容易引起误解。compare()会更好。此外,如果函数的返回类型是布尔型(true: a < b),那么可以使用std:: lexicographicoleical_compare(),代码会简单得多。