【问题标题】:sorting a vector of class in descending with std::sort使用 std::sort 对类向量进行降序排序
【发布时间】:2014-02-28 06:06:23
【问题描述】:

我有一个名为 HPC_user 的类,并尝试使用其成员变量活动之一按降序对 HPC_user 向量进行排序。似乎 std::sort 应该使用第三个参数来完成这项工作。所以我跟随这​​个线程并定义了一个更大的新结构。

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());
//.....
}

当我编译代码时,我得到了一些错误:

    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,

我做错了什么?对类向量进行排序的正确方法是什么?

【问题讨论】:

    标签: c++ class sorting vector


    【解决方案1】:

    你想要这个:

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

    【讨论】:

    • 这确实消除了错误。我真的很想对向量的一部分进行排序。所以我把它改成了users.begin(),users.begin()+len,现在就可以了。
    【解决方案2】:

    std::sort 与迭代器或指针一起使用,因此您的选择是:

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

    使用 &users[n] 表单,在元素中指定子范围会稍微容易一些。

    迭代器提供了更多的抽象——使用正确的迭代器,您可以对一个没有在内存中连续存储其数据的容器进行排序,就像标准要求std::vectors 那样。如果您开始使用迭代器,那么如果您稍后更改为使用这样的容器,您甚至不需要更正排序线,并且如果您忘记了它也不会在运行时出现异常行为。因此,最好将迭代器与标准算法一起使用,并在您自己的代码中采用类似的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      相关资源
      最近更新 更多