C++对类类型向量进行排序
我有一个名为 Recipes.h 的结构和一个名为 vector
。该向量在每个元素中包含 1 个 int 和 2 个字符串(一个字符串厨师名称和一个称为说明的字符串)。不过,我只想按字符串 Chef_name 对整个向量进行排序。我尝试做这样的事情
sort(recipes.begin(),recipes.end(),compare);
bool Menu::compare(const recipes* lhs, const recipes* rhs)
但它说食谱不是类型名称。我该如何对这个向量进行排序?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您发布的非常短的代码片段中可以看出,您首先使用
recipes
作为对象,然后作为类型。您的比较函数可能需要作为参数Recipes > const& 代替。请注意,如果操作不依赖于
Menu
类,最好将此函数声明为static
成员函数。函数签名应该是:
然后您将像这样使用它:
最后两个语句是相同的,我认为后者对于
compare
更明确。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 parametersRecipes > const&
instead. Note that if the operation does not depend on theMenu
class it would be better to declare this function as astatic
member function.The function signature should be:
and you would then use it like this:
Both last statements are the same, I think the later is more explicit about
compare
.