I'm trying to sort vectors of 2 structs of different number of element:
struct1 {
int id1;
int id2;
string name;
double ts1;
double ts2;
}
struct2 {
int id1;
int id2;
//string name; <-- this was left out
double ts1;
double ts2;
}
std::vector<struct1> vec1;
std::vector<struct2> vec2;
When I tried to sort vec1 and vec2 based on ts1, there is a big difference in the sorting time. The sizes of vec1 and vec2 are large (>100k elements). Does the size of struct affect the sorting?
EDIT: my sorting function
inline bool sorting(const Type &lhs, const Type &rhs) {
if (lhs.ts1 < rhs.ts1) {return true;}
else {return false; }
}
std::sort(vec.begin(),vec.end(),
[this] (Type lhs, Type rhs) { return sorting(lhs,rhs); });
struct2is much faster than manually have to call the assignment operator as instruct1forstd::string.sortingfunction could just bereturn lhs.ts1 < rhs.ts2;.stringelements have to be created, which is obviously catastrophic. Ha An Tran, try replacing[this] (Type lhs, Type rhs)with[this] (const Type& lhs, const Type& rhs)and let us know what you see.