【发布时间】:2017-05-04 01:30:49
【问题描述】:
我在地图中使用字符串作为键值并尝试自定义比较函数。当我通过比较字符串的长度来自定义比较函数时,地图无法告诉具有相同大小的不同字符串。代码如下:
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;
}
};
如果我的输入 strs 是 ["aba","cdc","eae","abcd"],则代码只会输出:"abcd" 和 "aba"。
【问题讨论】:
-
了解“这个”吗?明白什么?
标签: c++ c++11 comparator stdstring stdmap