I am using string as key value in map and trying to custom the comparison function. When I custom the comparison function by comparing the length the the strings, the map fail to tell the different strings with same size. Could someone help understand this? The code is as followed:
class Solution {
public:
int findLUSlength(vector<string>& strs) {
if(strs.size() < 2) return -1;
auto cmpByStringLength = [](const string &s1, const string &s2)->bool
{
return s1.size() < s2.size();
};
map<string, int, decltype(cmpByStringLength)> mpstringcount(cmpByStringLength);
for(int i = 0; i < strs.size(); i++)
mpstringcount[strs[i]]++;
for(auto itmp = mpstringcount.begin(); itmp != mpstringcount.end(); itmp++)
{
cout << "itmp->first: " << itmp->first << endl;
}
return -1;
}
};
If my input strs is ["aba","cdc","eae","abcd"], the code will just output: "abcd" and "aba".
Aucun commentaire:
Enregistrer un commentaire