简体   繁体   English

获取两个最大值的索引

[英]Get index of two biggest values

I got an array like myArray[4] = { 10, 3, -5, 30 }; 我有一个像myArray[4] = { 10, 3, -5, 30 };的数组myArray[4] = { 10, 3, -5, 30 }; and I'D like to get the indexes of it's two elements with the biggest value (element in this case 30, 10 ) and the indexe of 30 is 3 while the index of 10 is 0 (zero based of course) . 我想获得索引的它与最大的值的两个元件(元件在这种情况下30, 10 )和的indexe 303而的索引100 (零基于当然)。

So the result should be {3, 0} because the index of the biggest element (30.0) is 3 and the index of the second biggest element (10.0) is 0. 所以结果应该是{3, 0}因为最大元素(30.0)的索引是3而第二大元素(10.0)的索引是0。


How to implement a easy working solution for the problem? 如何实现一个简单易用的解决方案?

double myArray[4] = { 10, 3, -5, 30 };
double biggestElement_1 = *std::max_element(std::begin(myArray), std::end(myArray));
int biggestElement_1_indx = find(std::begin(myArray), std::end(myArray), biggestElement_1) - std::begin(myArray);
cout << biggestElement_1_indx << endl;

// How to go on to find the second biggest element ?

Note : I'm obviously new to C++ so sorry for the (stupid) beginner question - however: Any help would be very appreciated. 注意 :我显然是C ++的新手 ,对于(愚蠢的) 初学者问题感到遗憾 - 但是:任何帮助都会非常感激。 :)

You cannot find the indexes of the two largest values easily with the <algoritm> 's functions (unless of course you're OK with altering the array). 使用<algoritm>的函数很容易找到两个最大值的索引(当然,除非您可以更改数组)。 You'd better roll your own. 你最好自己动手。

template <class InputIt>
auto two_biggest(InputIt begin, InputIt end)
{
    auto result = std::make_tuple(0, 0);
    // iter in [begin; end) and save result.get<0> and result.get<1>
    // let as an exercise (it's an exercise, right?)
    return result;
}

You then will be able to call it with: 然后,您可以使用以下命令调用它:

auto t = two_biggest(std::begin(arr), std::end(arr));

or better yet, with C++17 tuple unpacking: 或者更好,使用C ++ 17元组解包:

[largest, second_largest] = two_biggest(std::begin(arr), std::end(arr));

Or for pre-C++17: 或者对于预C ++ 17:

int largest, second_largest;
std::tie(largest, second_largest) = two_biggest(std::begin(arr), std::end(arr));

You can use vector of pairs to save your array with their index, then sort them based on the value, and return the biggest values as you want, like this 您可以使用vector of pairs来保存数组及其索引,然后根据值对它们进行排序,并根据需要返回最大值,如下所示

#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
int main() {
  double myArray[4] = { 10, 3, -5, 30 };
  vector<pair<double, double > >vec;
  for(int i=0;i<4;i++){
    vec.push_back(make_pair(myArray[i],i));
  }

  sort(vec.begin(), vec.end());

  for(int i=0;i<vec.size();i++){
    cout<<vec[i].first<<" "<<vec[i].second<<endl;
  }

  cout<< vec[vec.size()-1].second<< ", "<<vec[vec.size()-2].second;
}

It's easy to find the largest value in the array: just scan through it, and keep track of the largest value that you've seen so far. 在阵列中找到最大值很容易:只需扫描它,并跟踪到目前为止看到的最大值。 When you reach the end, that's the largest value. 当你到达终点时,这是最大的值。

Finding the second largest value is just as simple: when you do the scan for the largest value, if you find a value that's larger than the largest value you've seen so far, that's the new largest value, and the largest value that you've seen so far is now the second largest value. 找到第二大值同样简单:当您扫描最大值时,如果您发现的值大于您目前所见的最大值,那么这是新的最大值, 也是您的最大值到目前为止看到的现在是第二大价值。

In code: 在代码中:

std::pair<int, int> get_two_largest(int* ptr, int size) {
int largest = std::numeric_limits<int>::min();
int second_largest;
for (int i = 0; i < size; ++i)
    if (largest <= ptr[size]) {
        second_largest = largest;
        largest = ptr[size];
    }
return std::pair<int, int>(largest, second_largest);
}

I used <= for the comparison because with plain < (which is what I would use to find the largest value), the function would return a bad result for an array with all equal values. 我使用<=进行比较,因为使用plain < (我将用于查找最大值),该函数将为具有所有相等值的数组返回错误结果。

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

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