下面的程序给出错误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作为错误功能
您可以将unordered_map作为参数传递给comp函数。这样,您将不会访问非静态对象。
以下代码可以正常工作: