我想使用指定的 equal_to 函数 int unordered_set 示例代码是这样的:
struct myEqual
{ //string with one different character is considered equal
bool operator()(const string &s1, const string &s2)const
{
if(s1.length() != s2.length()) return false;
int dis = 0;
for(unsigned int i = 0; i<s1.size(); i++)
{
if(s1[i] != s2[i])
{
dis++;
if(dis >= 2) return false;
}
}
return true;
}
};
int main()
{
unordered_set<string, std::tr1::hash<string>, myEqual> myDict;
myDict.insert("a");
myDict.insert("b");
myDict.insert("c");
unordered_set<string, std::tr1::hash<string>, myEqual>::iterator it = myDict.find("k");
if(it == myDict.end())
{
cout<<"myequal not work"<<endl;
}
else
{
cout<<*it<<endl;
}
return 0;
}
根据 myEqual 函数,“a”、“b”、“c”三个值“等于”“k”,但是 find 只返回一个迭代器。反正有没有找到所有相等的价值?