这是我的代码
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
/*
struct greater
{template<class T>
bool operator()(T const &a, T const &b) const { return a > b; }
};*/
//std::sort(numbers.begin(), numbers.end(), greater());
int main(){
vector<int,::greater<int>()> a;
int x;
while (cin >> x)
a.push_back(x);
sort(a.begin(),a.end());
for (int b : a){
cout << b << endl;
}
return 0;
}
为什么这是错误的?
map<int,int,::greater<int>()> a;
我看过一些博客,他们可以通过,但我不能 我想知道答案
最佳答案
std::map 所需的谓词(comporator)和 std::set比较容器的元素。默认情况下它将是 std::less . std::vector不需要比较器。
您需要更正以下行
vector<int,::greater<int>()> a;
到
vector<int> a;
如果你想按升序排序,你可以传递谓词std::greater作为参数之一如下:
std::sort(a.begin(), a.end(), std::greater<int>())
关于c++ - "greater"仿函数给出编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42941702/