L
Lambda
I defined a class:
class inverted_index
{
private:
std::map<std::string, std::vector<size_t> > index;
public:
std::vector<size_t> intersect(const std::vector<std::string>&);
};
bool compare(const std::string&, const std::string&);
vector<size_t> inverted_index::intersect(const vector<string>& query)
{
sort(query.begin(), query.end(), compare);
vector<size_t> result = index[query.front()];
return result;
}
bool compare(const string& s1, const string& s2)
{
return index[s1].size() < index[s2].size();
}
In the intersect function, I want to sort the query by the
string matching vector size. So I define a compare function fort
'sort'.
I think I can't define compare as member function of inverted_index,
there is a current object as a implicit parameter for member
functions.
And the compare will have three parameters. I think it's wrong!
But if I define compare as a nonmember function with two parameters,
how can it access the index object. Make it 'friend' is useless here.
class inverted_index
{
private:
std::map<std::string, std::vector<size_t> > index;
public:
std::vector<size_t> intersect(const std::vector<std::string>&);
};
bool compare(const std::string&, const std::string&);
vector<size_t> inverted_index::intersect(const vector<string>& query)
{
sort(query.begin(), query.end(), compare);
vector<size_t> result = index[query.front()];
return result;
}
bool compare(const string& s1, const string& s2)
{
return index[s1].size() < index[s2].size();
}
In the intersect function, I want to sort the query by the
string matching vector size. So I define a compare function fort
'sort'.
I think I can't define compare as member function of inverted_index,
there is a current object as a implicit parameter for member
functions.
And the compare will have three parameters. I think it's wrong!
But if I define compare as a nonmember function with two parameters,
how can it access the index object. Make it 'friend' is useless here.