这是我的比较函数
bool cmp(string* s1,string* s2)
{return(strlen(s1)>strlen(s2));}
这是我的Vector数组
vector<string>v(n);
这是我的排序函数
sort(v.begin(),v.end(),cmp);
我哪里出错了?
发布于 2018-06-09 03:13:10
我哪里出错了?
比较函数should have签名类似如下:
bool cmp(const Type1 &a, const Type2 &b);
因此,您的比较函数可能如下所示:
bool cmp(std::string const& s1, std::string const& s2)
{
return s1.length() > s2.length();
}
它起作用了,请说明它起作用的原因
它之所以有效,是因为std::sort
将在每轮中获取2个向量元素,并将它们发送到比较函数。向量由std::string
组成,您可以选择通过值、引用或const
引用来传递它们,但不能像您尝试的那样传递指针(为此,您需要std::vector<std::string *>
,有点无意义的tbh)
https://stackoverflow.com/questions/50766847
复制相似问题