C++ Sort Class Type Vector
I have a struct named Recipes.h and开发者_开发知识库 a vector called vector<Recipes> recipes. The vector contains 1 int, and 2 strings in each element (a string chef name and a string called instructions). However I want to sort the whole vector ONLY by the string chef_name. I tried doing something like this
sort(recipes.begin(),recipes.end(),compare);
bool Menu::compare(const recipes* lhs, const recipes* rhs)
But it says recipes is not a type name. How do I go about sorting this vector?
From the very short snip of code you posted, it can be seen that you use recipes first as an object and then as a type. Your comparison function probably wants as parameters Recipes > const& instead. Note that if the operation does not depend on the Menu class it would be better to declare this function as a static member function.
The function signature should be:
static bool Menu::compare(const Recipes& lhs, const Recipes& rhs)
and you would then use it like this:
sort(recipes.begin(),recipes.end(),compare); ...or...
sort(recipes.begin(),recipes.end(),&Menu::compare);
Both last statements are the same, I think the later is more explicit about compare.
加载中,请稍侯......
精彩评论