【发布时间】:2016-01-05 04:25:39
【问题描述】:
我已经定义了一个类
class Rent
{
public:
int s_time, duration, price, e_time;
Rent(int s, int d, int p)
{
s_time = s;
duration = d;
price = p;
e_time = s + d;
}
bool operator<(Rent const &r1)
{
return e_time < r1.e_time;
}
};
希望根据e_time 对其进行排序,所以我在Rent 上定义了<,但是,我不断收到错误
rent.cpp:38:12: error: no match for ‘operator+’ (operand types are ‘std::vector<Rent>’ and ‘int’)
sort(R, R+n);
^
当我尝试sort(R, R+n); 时。 R 是 Rent 类型的向量,n 是整数(向量的大小)。
除了上述之外,我尝试了这两种方法,但仍然失败!
sort(R, R + sizeof(R)/sizeof(R[0]));
sort(R.begin(), R.end());
我用 google 搜索并得到了一些使用 lambdas 的解决方案,但 sort() 的第二个参数再次是 int + custom_datatype 类型。
任何帮助都会很棒。
【问题讨论】:
-
仅供参考,您应该使用 initializer_list 而不是在构造函数的主体中执行复制分配。