简体   繁体   English

用std :: sort降序对类的向量进行排序

[英]sorting a vector of class in descending with std::sort

I have this class called HPC_user and try to sort a vector of HPC_user in descending order with one of its member variables activity. 我有一个名为HPC_user的类,并尝试使用其成员变量活动之一对HPC_user的向量进行降序排序。 It seems std::sort should do the job with a third argument. 似乎std :: sort应该使用第三个参数来完成这项工作。 So I followed this thread and defined a new struct greater. 因此,我遵循了这个线程,并定义了一个更大的新结构。

Sorting a vector in descending order 按降序对向量排序

struct cpuComp
{
  bool operator()(HPC_user const & a, HPC_user const & b)
  {
  return a.get_activity() > b.get_activity();
  }
};

int main()
{
  // do something;
    std::vector<HPC_user> users = db2class(ins, days);
  std::sort(users[0], users[len], cpuComp());
//.....
}

When I compiled the code, I got some errors: 编译代码时,出现一些错误:

    In file included from db2class.cpp:3:
    In file included from /usr/include/c++/4.2.1/iostream:44:
    In file included from /usr/include/c++/4.2.1/ostream:44:
    In file included from /usr/include/c++/4.2.1/ios:44:
    In file included from /usr/include/c++/4.2.1/bits/char_traits.h:45:
    In file included from /usr/include/c++/4.2.1/bits/stl_algobase.h:74:
    /usr/include/c++/4.2.1/bits/stl_iterator_base_types.h:128:35: error: no type named 'iterator_category' in 'HPC_user'
          typedef typename _Iterator::iterator_category iterator_category;
                  ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
    /usr/include/c++/4.2.1/bits/stl_algo.h:2854:24: note: in instantiation of template class 'std::iterator_traits<HPC_user>'
          requested here
          typedef typename iterator_traits<_RandomAccessIterator>::value_type
                           ^
    db2class.cpp:119:3: note: in instantiation of function template specialization 'std::sort<HPC_user, bool (*)(const HPC_user &,
          const HPC_user &)>' requested here
      std::sort(users[0], users[len], cpuComp);
      ^
In file included from db2class.cpp:3:
In file included from /usr/include/c++/4.2.1/iostream:44:
In file included from /usr/include/c++/4.2.1/ostream:44:
In file included from /usr/include/c++/4.2.1/ios:47:
In file included from /usr/include/c++/4.2.1/bits/ios_base.h:46:
In file included from /usr/include/c++/4.2.1/bits/locale_classes.h:46:
In file included from /usr/include/c++/4.2.1/string:56:
In file included from /usr/include/c++/4.2.1/algorithm:67:
/usr/include/c++/4.2.1/bits/stl_algo.h:2864:19: error: invalid operands to binary expression ('HPC_user' and 'HPC_user')
      if (__first != __last)
          ~~~~~~~ ^  ~~~~~~
db2class.cpp:122:3: note: in instantiation of function template specialization 'std::sort<HPC_user, cpuComp>' requested here
  std::sort(users[0], users[len], cpuComp());
  ^
/usr/include/c++/4.2.1/bits/postypes.h:206:5: note: candidate template ignored: failed template argument deduction
    operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
    ^
/usr/include/c++/4.2.1/bits/stl_pair.h:109:5: note: candidate template ignored: failed template argument deduction
    operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
    ^
/usr/include/c++/4.2.1/bits/stl_iterator.h:294:5: note: candidate template ignored: failed template argument deduction
    operator!=(const reverse_iterator<_Iterator>& __x,
    ^
/usr/include/c++/4.2.1/bits/stl_iterator.h:344:5: note: candidate template ignored: failed template argument deduction
    operator!=(const reverse_iterator<_IteratorL>& __x,
    ^
/usr/include/c++/4.2.1/bits/allocator.h:120:5: note: candidate template ignored: failed template argument deduction
    operator!=(const allocator<_T1>&, const allocator<_T2>&)
    ^
/usr/include/c++/4.2.1/bits/basic_string.h:2188:5: note: candidate template ignored: failed template argument deduction
    operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,

What did I do wrong? 我做错了什么? What is the correct way to sort a vector of classes? 对向量分类的正确方法是什么?

你要这个:

std::sort(users.begin(), users.end(), cpuComp());

std::sort works with iterators or pointers, so your options are: std::sort可与迭代器或指针一起使用,因此您的选择是:

std::sort(users.begin(), users.end(), cpuComp());

std::sort(&users[0], &users[len], cpuComp());  // assuming len == users.size()

With the &users[n] form, it's slightly easier to specify a sub-range within the elements. 使用&users [n]表单,在元素内指定子范围会稍微容易一些。

Iterators provide a bit more abstraction - with the right iterator, you could sort a container that didn't store its data contiguously in memory the way the Standard requires std::vector s to. 迭代器提供了更多的抽象-使用正确的迭代器,您可以对一个容器进行排序,该容器不会像标准要求std::vector那样将其数据连续存储在内存中。 If you start off using iterators, then should you change to use such a container later you won't even need to correct the sorting line, and it won't misbehave at run-time if you forget. 如果您从使用迭代器开始,那么以后应该更改为使用这样的容器时,您甚至不需要更正排序行,并且即使您忘记它也不会在运行时出现异常。 So, it's best to use iterators with the Standard algorithms and take a similar approach with your own code. 因此,最好将迭代器与Standard算法配合使用,并对您自己的代码采用类似的方法。

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

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