简体   繁体   English

如何比较两个相等但一个按升序而另一个按降序排列的 arrays?

[英]How do I compare two arrays that are equal but one is in ascending order and other is in descending order?

int array1[] = {1,2,3,4,5};
int array2[] = {5,4,3,2,1};
if (std::equal(std::begin(array1), std::end(array1), std::begin(array2)))
    cout << "Arrays are equal.";
else
    cout << "Arrays are not equal.";

Now both the arrays are equal but one is in ascending and the other one is descending so the output with this code is "Arrays are not equal", how can we output "Arrays are equal"?现在两个 arrays 是相等的,但一个是上升的,另一个是下降的,所以带有这个代码的 output 是“数组不相等”,我们怎么能 Z78E6221F6393D1356681DB398F14CE6D “数组不相等”?

Use reverse_iterator :使用reverse_iterator

std::equal(std::begin(array1), std::end(array1), std::rbegin(array2))

If you know that one is always in ascending order, and the other in descending order then it's pretty easy to use forward iterators for one and reverse iterators for the other.如果您知道一个始终按升序排列,而另一个始终按降序排列,则很容易对一个使用正向迭代器,对另一个使用反向迭代器。

My guess would be that inverted order was only an example of containing the same data, but in a different order, and they both happened to be in ascending order, you'd still want them to compare as equal.我的猜测是,倒序只是包含相同数据的一个示例,但顺序不同,而且它们恰好都是升序,你仍然希望它们比较相等。 And likewise, if both were in random order, but still contained exactly the same elements, you'd want that counted as equal as well.同样,如果两者都按随机顺序排列,但仍包含完全相同的元素,那么您也希望将其视为相等。

In this case, you're pretty much stuck with comparing sorted copies of the two inputs.在这种情况下,您几乎只能比较两个输入的排序副本。 In theory you could step through one, and search for the elements in the other, but that's slow, and requires quite a bit of care to get correct (eg, unless you're careful, it's easy to end up with an implementation that says [1, 1, 2] is equal to [1, 2, 2] ).从理论上讲,您可以逐步完成一个,然后在另一个中搜索元素,但这很慢,并且需要非常小心才能正确(例如,除非您很小心,否则很容易最终得到一个说[1, 1, 2]等于[1, 2, 2] )。 Worse, even if you get this really correct, you still end up with an O(N 2 ) algorithm, which gets pretty slow in a hurry if you have very many elements.更糟糕的是,即使你得到了这个非常正确的结果,你仍然会得到一个 O(N 2 ) 算法,如果你有很多元素,它会很快变得很慢。

So, if you really need to support this, the most practical method is probably going to be to create a copy of each input, sort those copies, and then compare the results to each other (though if you can sort the originals, that obviously reduces memory usage).所以,如果你真的需要支持这一点,最实用的方法可能是为每个输入创建一个副本,对这些副本进行排序,然后将结果相互比较(尽管如果你可以对原件进行排序,那显然减少 memory 的使用)。

If you're dealing with large arrays, you might also consider counting the number of times each value occurs using something like an std::unordered_map .如果您正在处理大型 arrays,您还可以考虑使用类似std::unordered_map的方式计算每个值出现的次数。 This reduces the expected complexity from O(N log N) to O(N), so with enough elements, it should be faster (but exactly how many is enough may be hard to guess, and likely varies between implementations).这将预期的复杂性从 O(N log N) 降低到 O(N),因此如果元素足够多,它应该会更快(但究竟有多少足够可能很难猜测,并且可能因实现而异)。

From C++20, you can use the ranges facilities to write the condition as:从 C++20 开始,您可以使用范围工具将条件编写为:

std::ranges::equal(array1, std::views::reverse(array2))

which expresses the intent much more directly, and clearly.它更直接、更清晰地表达了意图。

See There are two approaches to solve the question.请参阅有两种方法可以解决该问题。

  1. Sort anyone array...for eg.对任何数组进行排序...例如。 sort descending array.对降序数组进行排序。 Then compare both the arrays.然后比较两个 arrays。 But sort function will take O(n log n) time.但是排序 function 将花费 O(n log n) 时间。

  2. Traverse (Faster Approach) - In this approach, let there be two array A1 and A2. Traverse (Faster Approach) - 在这种方法中,让有两个数组 A1 和 A2。 So travel in A1 from index 0 to length of A1.因此,在 A1 中从索引 0 到 A1 的长度。 and travel in A2 from the length of A2 to 0. if elements are equal then yes both arrays have the same elements else not.并在 A2 中从 A2 的长度移动到 0。如果元素相等,那么是的,两个 arrays 具有相同的元素,否则没有。

int array1[]= {1,2,3,4,5}
int array2[]= {5,4,3,2,1}

bool is_same = true;

for(int i=0; i<5; i++)
{
    if(array1[i] != array2[4-i])
    {
        is_same = false;
        break;
    }
}
if (is_same)
    cout << "Arrays are equal.";
else
    cout << "Arrays are not equal.";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM