【发布时间】:2019-12-30 09:18:55
【问题描述】:
下面的程序给出错误invalid use of mep in static function
当我将 mep 也声明为静态给出错误 undefined reference to mep
当我声明 comp 为非静态且 mep 为非静态时
我给出错误invalid use of non static member in sort
我应该怎么做才能在 leetcode 中提交这个解决方案类?
class Solution {
public:
unordered_map<char,int>mep;
static bool comp(string a,string b){
int n = min(a.size(),b.size());
for(int i=0;i<n;i++){
int diff = mep[a[i]]-mep[b[i]];
if(diff<0)return false;
if(diff>0)return true;
}
return true;
}
bool isAlienSorted(vector<string>& words, string order) {
for(int i=0;i<order.size();i++){
mep[order[i]]=i;
}
vector<string>temp;
temp=words;
sort(temp.begin(),temp.end(),comp);
return temp==words;
}
};
我知道比较器的其他方法可以是 lambda 函数,哪一个是有效的上述或 lambda?
【问题讨论】:
-
mep是类的非静态成员,因此只能在类的静态成员中用作实例的成员(例如diff = some_solution.mep[a[i]] - some_solution.mep[b[i]]) - 比如comp(). -
您在排序时需要
mep作为临时助手吗?
标签: c++ sorting comparator