如何找到 n 个最大元素的索引

How to find indexes of the n greatest elements

本文关键字:元素 索引 何找      更新时间:2023-10-16

我有一个任意类型的容器(Vector(,我想得到一个包含n个最大(或最小(元素索引的向量。

有没有标准的方法?

这正是

本周大师之一的主题 http://www.gotw.ca/gotw/073.htm

正在报告首选的解决方案,但是,我强烈建议您阅读这篇文章(以及一般的博客(,它真的很好。

#include <vector>
#include <map>
#include <algorithm>
namespace Solution3
{
  template<class T>
  struct CompareDeref
  {
    bool operator()( const T& a, const T& b ) const
    { return *a < *b; }
  };

  template<class T, class U>
  struct Pair2nd
  {
    const U& operator()( const std::pair<T,U>& a ) const
    { return a.second; }
  };
  template<class IterIn, class IterOut>
  void sort_idxtbl( IterIn first, IterIn last, IterOut out )
  {
    std::multimap<IterIn, int, CompareDeref<IterIn> > v;
    for( int i=0; first != last; ++i, ++first )
      v.insert( std::make_pair( first, i ) );
    std::transform( v.begin(), v.end(), out,
                Pair2nd<IterIn const,int>() );
  }
}
#include <iostream>
int main()
{
  int ai[10] = { 15,12,13,14,18,11,10,17,16,19 };
  std::cout << "#################" << std::endl;
  std::vector<int> aidxtbl( 10 );

  // use another namespace name to test a different solution
  Solution3::sort_idxtbl( ai, ai+10, aidxtbl.begin() );

  for( int i=0; i<10; ++i )
  std::cout << "i=" << i
            << ", aidxtbl[i]=" << aidxtbl[i]
            << ", ai[aidxtbl[i]]=" << ai[aidxtbl[i]]
            << std::endl;
  std::cout << "#################" << std::endl;
}