sorting - C++ - invalid operands to binary expression -
i trying use std::sort sort list of structs. getting error:
invalid operands binary expression ('std::__1::__list_iterator<process, void *>' , 'int') __sort3<_compare>(__first, __first+1, __j, __comp);
struct:
struct process { int process_id; int cpu_cycles; int mem_footprint; };
main function:
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 = 0; < 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; }
i want sort process items in list cpu_cylcles value in ascending order. made compare_pid function takes in 2 process , returns boolean. can't figure out error is. can please help?
invalid operands binary expression
('std::__1::__list_iterator' , 'int')
__sort3<_compare>(__first, __first+1, __j, __comp);
alright, third line of code somwhere in std::sort
. operator there +
. expression error __first+1
__first
list<process>::iterator
. list iterators not have iterator+int
overload, because bidirectional iterators, not random access.
you cannot call std::sort
on std::list
. use std::vector
instead, or container random access iterators.
Comments
Post a Comment