简体   繁体   English

使用 .find() 和 struct 作为 C++ 映射中的键

[英]Using .find() with struct as key in map in C++

I do not have access to BOOST or STL;我无权使用 BOOST 或 STL; my struct and map looks similar to the following psuedo:我的结构和地图看起来类似于以下伪:

 struct s_map_key{
    int a;
    int b;
    bool operator<(const s_map_key& smk) const 
    {
        if (a < smk.a)       
        {            
            return true;
        } else if (a == smk.a)  
        { 
            if (b < smk.b) 
            { 
                return true;
            } 
            else if (b == smk.b)
            {
                return true;
            }
        } 
            return false;
    }
};

int main(int argc, char* argv[])
{

    std::multimap<s_map_key, std::string> myMap;
    for(int i = 0; i <10; i++)
    {
    s_map_key smk;
    smk.a = i;
    smk.b = 2;
    myMap.insert(std::make_pair(smk, "test"));
    }

    s_map_key smk;
    smk.a = 3;
    std::multimap<s_map_key, std::string>::iterator x = myMap.find(smk);
    if(x != myMap.end())
    {
        std::cout << x->first.a <<std::endl;
        std::cout << x->first.b <<std::endl;
    }
    return 0;
}

What I am trying to do is search my multimap for all cases where A = 2, B = 2, or A & B = 2. I am not exactly sure but, I think i need to create the predicates in my struct for "finding".我想要做的是在 A = 2、B = 2 或 A & B = 2 的所有情况下搜索我的多重映射。我不太确定,但是,我认为我需要在我的结构中创建谓词以“查找”。 Ideas?想法?

operator< is all you need for find or anything else. operator<是您find或其他任何所需的全部内容。 However, your implementation has a bug.但是,您的实现有一个错误。 It returns true if the operands are equal.如果操作数相等,则返回true

find and other Standard Library components use the assumption that a == b iff ! (a < b) && ! (b < a) find和其他标准库组件使用a == b iff ! (a < b) && ! (b < a) a == b iff ! (a < b) && ! (b < a) a == b iff ! (a < b) && ! (b < a) , so if a and b are equal, < must be false for find to work. a == b iff ! (a < b) && ! (b < a) ,所以如果ab相等, <必须为false才能find工作。

        else if (b == smk.b)
        {
            return false; // was true
        }

Unless you want to establish an equivalence of 2 == smk if (smk.a == 2 || smk.b == 2 || smk.a & smk.b == 2), I suggest making the predicate external and using std::find_if.除非你想建立 2 == smk if (smk.a == 2 || smk.b == 2 || smk.a & smk.b == 2) 的等价,我建议使谓词外部并使用std::find_if。 If you make it part of the struct so that you can use multimap's internal find function, you could open the door for the predicate being used in an undesired way by another coder.如果您将其作为结构的一部分以便您可以使用 multimap 的内部 find 函数,您可能会为其他编码人员以不希望的方式使用谓词打开大门。 Also, the internal find function will only return the first instance that matches, whereas with std::find_if you can update your starting point so that you can find all instances.此外,内部 find 函数将只返回匹配的第一个实例,而使用 std::find_if 可以更新起点,以便找到所有实例。 If you want to use the internal functions, you'll need to use equal_range instead.如果要使用内部函数,则需要改用 equal_range。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM