New answers tagged

2

Couple of small things: Use emplace_back rather than push_back when you just have the parameters for the constructors: longList.push_back(Task{ taskInput }); // This is better written as: longList.emplace_back(taskInput); The difference between the two: push_back(Task{ taskInput });. This creates a "Task" object as an input parameter. It then calls ...


4

Loops for (int i = 0; i < item_switch.size(); i++) { Your loops have a common problem: the correct type for traversing a std::vector<T> via index is std::vector<T>::size_type (std::size_t is fine too). However, a better solution is to eliminate loops altogether using std::inner_product (defined in header <numeric>) and std::plus (...


9

Since you declare a three parameter constructor, the default constructor will not be implicitly defined, so it does not need to be explicitly deleted. There's no real harm in providing a default constructor, since you have initializers for all your members. Alternatively, since the only constructor you supply requires three parameters that initialize all ...


3

public: is excess in a struct. Members directly initialized in ctors are better directly initialized: Job(int index, int time_slot, int profit) : index(index), time_slot(time_slot), profit(profit) {} only more complex logic should go in ctor's body, if any. As a matter of fact, if(cond) { return true; } return false; is actually ...


4

The Job class has public members, so it's simpler to make it an aggregate: struct Job { int index; int time_slot; int profit; }; Then, use std::tie to simplify the comparison operators: (operands to these operators should be marked as const, which you didn't) bool operator<(const Job& lhs, const Job& rhs) { return std::tie(lhs....


5

In the sort() function its currently hardcoded to merge sort which I assume is not intentional. However, if you do allow the user to give an input you should do some error checking. You'll currently get unexpected behaviour if the user enters anything other than 1 or 2. You should use std::swap rather than implementing your own swap function. Some types can ...


-2

READABILITY Function: int count(T* array) I don't prefer this way of acessing a (T) while(*(array+i)), because of readability reasons. Better way: while(array[i]) PERFORMANCE Function: void swap(T& t1, T& t2) Faster way is to use XOR (^) swap algorithm. it is faster and small amout of memory is used. if (t1 != t2) { *t1 ^= *t2; *t2 ^= ...


Top 50 recent answers are included