简体   繁体   English

C++:仅在具有指定索引的元素中查找数组中的最小值

[英]C++: Find minimum in array only among elements with specified indices

I couldn't find a solution to the following problem:我找不到以下问题的解决方案:

I am trying to find the minimum in a C++ array, but without looking at all elements inside, only among elements that have a specified index.我试图在 C++ 数组中找到最小值,但不查看内部的所有元素,只查看具有指定索引的元素。 Eg, if I am given an index k, and an array int* mylist = int[n] , then I want to look for the minimal element in mylist only in a "sublist" of elements, where eg the index of the respective elements i fulfills i%n = k .例如,如果给我一个索引 k 和一个数组int* mylist = int[n] ,那么我只想在元素的“子列表”中查找 mylist 中的最小元素,例如各个元素的索引我满足i%n = k In Python, this can be easily solved by在 Python 中,这可以通过以下方式轻松解决

min([mylist[i]  for i in range(n) if i%n==k]),

but afaik, there is no equivalent in C++.但是 afaik,在 C++ 中没有等价物。 Also, lambda functions do not do the trick, as far as I understood.此外,据我所知,lambda 函数并不能解决问题。

Any idea how to do this efficiently?知道如何有效地做到这一点吗? Any advice would be appreciated!任何意见,将不胜感激!

This is an example of why standard algorithms work with iterators rather than directly with containers/collections.这是为什么标准算法与迭代器一起工作而不是直接与容器/集合一起工作的一个例子。 It allows you to write an iterator that embodies the indices you care about.它允许您编写一个包含您关心的索引的迭代器。

The problem is that writing an iterator is more work than most people would like, or consider justified for this simple of a task.问题是编写迭代器比大多数人想要的工作量多,或者认为对于这个简单的任务是合理的。 Boost has some utilities to make it easier to implement iterators, such as a filter iterator , but as far as I know these are applied based only on the content of the collection, not the indices. Boost 有一些实用程序可以更轻松地实现迭代器,例如过滤器迭代器,但据我所知,这些仅基于集合的内容而不是索引来应用。

The code involved in creating an index_filter would be fairly trivial (eg, should be easy to extrapolate from the code for filter_iterator linked above), but there's more boiler-plate there than most people would like.创建index_filter所涉及的代码相当简单(例如,应该很容易从上面链接的filter_iterator代码推断出来),但是那里的样板代码比大多数人想要的要多。 Unless you're doing things like this pretty regularly, it may be difficult to justify.除非你经常做这样的事情,否则可能很难证明是合理的。

With range-v3 , you may do something similar to:使用range-v3 ,您可以执行以下操作:

for (auto e : v | ranges::view::drop(3) | ranges::view::stride(5)) {
    std::cout << e << std::endl;   
}

so you iterate over index 5 * k + 3 .所以你迭代索引5 * k + 3

And you can call ranges::min_element on resulting view:您可以在结果视图上调用ranges::min_element

auto view = v | ranges::view::drop(3) | ranges::view::stride(5);
auto it = ranges::min_element(view);
std::cout << *it << std::endl;

Demo演示

i % n = k will be a either: i % n = k将是一个:

  1. k if k < n k 如果k < n
  2. [] empty that is, if k > n []空,即如果k > n

This is because i varies from 0 to n这是因为 i 从0 to n

Hence there will be only one or no elements.因此,将只有一个元素或没有元素。

For clarity: n = 50; print([i%n for i in range(n)])为清楚起见: n = 50; print([i%n for i in range(n)]) n = 50; print([i%n for i in range(n)]) , gives: n = 50; print([i%n for i in range(n)]) ,给出:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]

That is, unique values between 0 to n-1也就是说, 0 to n-1之间的唯一值

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

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