0

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. It seems std::sort should do the job with a third argument. 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?

Community
  • 1
  • 1
ddd
  • 4,665
  • 14
  • 69
  • 125

2 Answers2

6

You want this:

std::sort(users.begin(), users.end(), cpuComp());
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • this indeed got rid of the errors. I really wanted to sort part of the vector. So I changed it to users.begin(), users.begin()+len and it works now. – ddd Feb 28 '14 at 06:14
0

std::sort works with iterators or pointers, so your options are:

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.

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::vectors to. 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.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252