简体   繁体   English

如何在搜索中不使用std :: map的自定义比较功能(map :: find)?

[英]How not to use custom comparison function of std::map in searching ( map::find)?

As you can see in my code, lenMap is a std::map with a custom comparison function . 如您在我的代码中lenMaplenMap是带有自定义比较功能std::map This function just check the string's length. 此函数仅检查字符串的长度。

Now when I want to search for some key ( using map::find ), the map still uses that custom comparison function. 现在,当我想搜索一些键(使用map::find )时,地图仍然使用该自定义比较功能。

But How can I force my map not to use that when I search for some key ? 但是,当我搜索某些密钥时,如何强制我的地图不使用它

Code: 码:

struct CompareByLength : public std::binary_function<string, string, bool>
{
    bool operator()(const string& lhs, const string& rhs) const
    {
        return lhs.length() < rhs.length();
    }
};

int main()
{
    typedef map<string, string, CompareByLength> lenMap;
    lenMap mymap;

    mymap["one"] = "one";
    mymap["a"] = "a";
    mymap["foobar"] = "foobar";

    // Now In mymap: [a, one, foobar]

    string target = "b";
    if (mymap.find(target) == mymap.end())
        cout << "Not Found :) !";
    else
        cout << "Found :( !"; // I don't want to reach here because of "a" item !

    return 0;
}

The map itself does not offer such an operation. 地图本身不提供这种操作。 The idea of the comparison functor is to create an internal ordering for faster lookup, so the elements are actually ordered according to your functor. 比较函子的想法是创建内部排序以加快查找速度,因此实际上根据您的函子对元素进行排序。

If you need to search for elements in a different way, you can either use the STL algorithm std::find_if() (which has linear time complexity) or create a second map that uses another comparison functor. 如果需要以其他方式搜索元素,则可以使用STL算法std::find_if() (具有线性时间复杂度),也可以创建使用另一个比较函子的第二张地图。

In your specific example, since you seem only to be interested in the string's length, you should rather use the length (of type std::size_t ) and not the string itself as a key. 在您的特定示例中,由于您似乎只对字符串的长度感兴趣,因此应该使用长度(类型为std::size_t )而不是字符串本身作为键。

By the way, std::binary_function is not needed as a base class. 顺便说一句,不需要std::binary_function作为基类。 Starting from C++11, it has even been deprecated, see here for example. 从C ++ 11开始,甚至不再使用它,例如,请参见此处

The comparison function tells the map how to order elements and how to differentiate between them. 比较功能告诉地图如何排序元素以及如何区分元素。 If it only compares the length, two different strings with the same length will occupy the same position in the map (one will overwrite the other). 如果仅比较长度,则具有相同长度的两个不同字符串将占据映射中的相同位置(一个将覆盖另一个)。

Either store your strings in a different data structure and sort them, or perhaps try this comparison function: 将您的字符串存储在其他数据结构中并对其进行排序,或者尝试使用此比较功能:

struct CompareByLength
{
    bool operator()(const string& lhs, const string& rhs) const
    {
        if (lhs.length() < rhs.length())
        {
            return true;
        }
        else if (rhs.length() < lhs.length())
        {
            return false;
        }
        else
        {
            return lhs < rhs;
        }
    }
};

I didn't test it, but I believe this will first order strings by length, and then however strings normally compare. 我没有进行测试,但是我相信这将首先按长度对字符串进行排序,然后通常将字符串进行比较。

You could also use std::map<std::string::size_type, std::map<std::string, std::string>> and use the length for the first map and the string value for the second map. 您还可以使用std::map<std::string::size_type, std::map<std::string, std::string>>并将长度用于第一张地图,并将字符串值用于第二张地图。 You would probably want to wrap this in a class to make it easier to use, as there is no protection against messing it up. 您可能希望将其包装在一个类中,以使其更易于使用,因为没有防止将其弄乱的保护措施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 std :: map比较功能 - Std::map comparison function 如何使用关联数据的自定义比较功能比较c ++ std :: map? - How to compare c++ std::map with custom comparison function for associated data? 如果没有逻辑方法定义比较运算符,如何将自定义类用作std :: map的键? - How to use a custom class as key with std::map if there is no logical way to have a comparison operator defined? 如何仅使用自定义比较器在 std::map 中进行搜索? - How to only make searching in std::map using a custom comparator? std :: find_if,std :: binary_function用于按值搜索std :: map - std::find_if , std::binary_function for searching std::map by value 如何使用std::map的Compare模板参数进行值比较? - How to use the Compare template parameter of std::map for value comparison? 搜索一个std :: Map - Searching a std::Map 隐式转换为比较 function object(仿函数)用于 std::sort 但不是 std::map? - Implicit conversion to comparison function object (functor) for std::sort but not std::map? std :: map,std :: equal_range和比较函数 - std::map, std::equal_range, and comparison function 如何为std :: map实现std :: map的比较逻辑,该逻辑包含两个“ Pos”结构,每个结构均包含x和y坐标 - How to implement std::map::find comparison logic for a std::map containing two 'Pos' structs that each contain x and y coordinates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM