Sorting function for a vector of objects based on different fields
问题 I have a vector of objects: struct Student{ string name; string id; string major; int age; }; vector<Student> s; Is there any way to write ONE general (maybe template) function to sort this vector (or array) based on different fields instead of writing four different functions? 回答1: I think the previous comments all said that it is possible to write such a comparison function. But if I understand you correctly, you want one function for all 4 comparisons (maybe in a templated way). There is