C++ 按降序c+对向量进行排序+;

C++ 按降序c+对向量进行排序+;,c++,sorting,dictionary,iterator,C++,Sorting,Dictionary,Iterator,我试着用学生的平均分数按降序排列向量,但我不知道正确的方法是什么。这是我的密码 void sortDes() { int len = students.size(); for(int i = 0; i < len; i++) { for(int j = 0;j < len - 1; j++) { if(students[j].aver

我试着用学生的平均分数按降序排列向量,但我不知道正确的方法是什么。这是我的密码

          void sortDes()
       {
       int len = students.size();
       for(int i = 0; i < len; i++)
       {
            for(int j = 0;j < len - 1; j++)
            {
              if(students[j].average()> students[j+1].average())
               {

                swap(students[j+1], students[j]);
               }
             }
        } 

       }
void sortDes()
{
int len=students.size();
对于(int i=0;i学生[j+1].average())
{
交换(学生[j+1],学生[j]);
}
}
} 
}

使用
std::sort
std::greater
如下:

#include <functional>
#include <vector>
#include <algorithm>
int main()
{
    std::vector<int> Vec {2,5,4,8,1,2,2};
    std::sort(Vec.begin(), Vec.end(), std::greater<int>());// After sort will be 8,5,4,2,2,2,1
    return 0;
}
class CStudent
{
public:
    bool operator > (CStudent& cmp1)
    {
        //Do your own calculations here
        if ( cmp1.val < val )
        {
            return true;
        }

        return false;
    }
private:
    int val;
};

你真的是指一个
std::map
——因为这个问题对这个问题没有意义——还是一个
std::vector
?我编辑它,确切地说它是一个矢量现在你已经把它改成了vector,使用
std::sort
,而不是可怕的冒泡排序。如果你在谷歌搜索中复制粘贴你问题的确切标题,它提供了几个非常好的答案…谢谢,但我通过了,更大的是未定义的,我应该创建更大的还是什么?@Joe add#include错误2错误C2664:'bool std::greater::operator()(constTy&,constTy&)const::无法将参数1从'CStudent'转换为'const int&'c:\程序文件(x86)\microsoft visual studio 12.0\vc\include\algorithm 3071 1Project1@Joe:您不是在对整数排序,而是在对自己的
struct
进行排序。请参阅。@Joe这是因为您有CStudent的容器,所以需要重写此类中的运算符>
class CStudent
{
public:
    bool operator > (CStudent& cmp1)
    {
        //Do your own calculations here
        if ( cmp1.val < val )
        {
            return true;
        }

        return false;
    }
private:
    int val;
};
//...
    std::sort(Vec.begin(), Vec.end(), [](CStudent& cmp1, CStudent& cmp2  )->bool{return cmp1 > cmp2;});
//...