Reputation: 129
The below program is giving as error invalid use of mep in static function
When i declaring mep also as static giving as error undefined reference to mep
when i am declaring comp as non static and also mep as non static
i am giving error invalid use of non static member in sort
what should I do I have to submit this solution class in 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;
}
};
I know other approach for comparator can be lambda function , which one is efficient the above or lambda?
Upvotes: 1
Views: 898
Reputation: 3187
You can pass the unordered_map as a parameter to the comp function. That way you won't be accessing the non-static object but it will require you to write your own sorting algorithm:
static bool comp(const unordered_map<char, int>& map_mep, string a, string b)
{
int n = min(a.size(), b.size());
for (int i = 0; i < n; i++) {
// this way there is no non-static involved
int diff = map_mep[a[i]] - map_mep[b[i]];
if (diff < 0)return false;
if (diff > 0)return true;
}
return true;
}
Because mep
is not a static member of the class a static function cannot see it. You should set mep
to be static
in order to fix this.
Upvotes: -1
Reputation: 66224
Declare a custom comparator type, using that as the groundwork for your eventual comparator argument for std::sort
. In the process, you gain re-usability; something sorely lacking with a static
implementation.
class Solution {
public:
struct Comp
{
unordered_map<char, int> mep;
bool operator()(std::string const& a, std::string const& b)
{
size_t n = min(a.size(), b.size());
for (size_t 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> const& words, string order)
{
Comp comp;
for (int i = 0; i<order.size(); i++) {
comp.mep[order[i]] = i;
}
vector<string>temp = words;
sort(temp.begin(), temp.end(), comp);
return temp == words;
}
};
Regarding your last question, compile to optimized code and measure with a sound benchmark (not as easy as it sounds). If there even is a noticeable difference, my money is on the lambda (which is pretty much what we have above anyway) for no other reason than because of the stronger likelihood the compiler will inline the comparator within the std::sort
expansion.
Upvotes: 2
Reputation: 8427
You cannot access a non-static member variable inside a static member function. In addition, you should define the static member variables outside of the class as well. The below code works fine, without any compilation warnings and errors.
#include <string>
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
static 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 (size_t i = 0; i < order.size(); i++) {
mep[order[i]] = i;
}
vector<string> temp;
temp = words;
sort(temp.begin(), temp.end(), comp);
return temp == words;
}
};
unordered_map<char, int> Solution::mep;
void main()
{
}
Upvotes: 1
Reputation: 6101
The below program is giving as error invalid use of mep in static function
Because static function (comp
) in C++ cannot access non-static variable (mep
)
When i declaring mep also as static giving as error undefined reference to mep
A static variable of a class need to have a definition not just declaration. So give mep
an inittialize.
Upvotes: 0