[英]C++ - invalid operands to binary expression
我正在尝试使用std :: sort对结构列表进行排序。 但是我得到了错误:
invalid operands to binary expression ('std::__1::__list_iterator<process, void *>' and 'int') __sort3<_Compare>(__first, __first+1, __j, __comp);
结构:
struct process {
int process_id;
int cpu_cycles;
int mem_footprint;
};
主功能:
int main() {
list<process> process_list;
init_process_list(process_list);
sort(process_list.begin(), process_list.end(), compare_pid);
}
init_process_list:
void init_process_list(list<process> &p_list) {
cout << "\n>> Generating process list...";
generator generate; // Random number generator class
process p;
for(int i = 0; i < process_count; i++) {
p.process_id = i;
p.cpu_cycles = generate.rand_num_between(cycle_lbound, cycle_ubound);
p.mem_footprint = generate.rand_num_between(mem_lbound, mem_ubound);
p_list.push_back(p);
}
cout << "Done" << endl;
}
compare_pid:
bool compare_pid(process &lhs, process &rhs) {
return lhs.cpu_cycles < rhs.cpu_cycles;
}
我想按照其cpu_cylcles值的升序对列表中的所有处理项目进行排序。 我还制作了compare_pid函数,该函数需要两个过程并返回一个布尔值。 我不知道是什么错误。 有人可以帮忙吗?
对二进制表达式无效的操作数
(“ std :: __ 1 :: __ list_iterator”和“ int”)
__sort3 <_Compare>(__ first,__first + 1,__j,__comp);
好的,第三行代码在std::sort
。 唯一的运算符是+
。 因此,带有错误的表达式是__first+1
,其中__first
是list<process>::iterator
。 列表迭代器没有iterator+int
重载,因为它们是双向迭代器,但不是随机访问。
您不能在std::list
上调用std::sort
。 请改用std::vector
或其他具有随机访问迭代器的容器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.