简体   繁体   English

当一对键不包含两个值时,如何将两个键(对)映射到一个键?

[英]How to map 2 keys (pair) to one key when the pair might not contain both values?

I want to map md5 to sha1 and sha256 to sha1. 我想将md5映射到sha1,将sha256映射到sha1。

Instead of using 2 different maps I thought of using std::map<pair<string, string>, string> . 我考虑使用std::map<pair<string, string>, string>而不是使用2个不同的映射。 When inserting vlaues, I would have both md5 and sha256, but I can query only with md5 for exampe. 插入vlaues时,我会同时拥有md5和sha256,但是我只能向md5查询示例。 Example : 范例:

md5= x;
map.find (x,null)--> return sha1
sha256 =y;
map.find ("" , y) --> return sha1
map(x,y) ----> return sha1

Is there any way of doing it? 有什么办法吗?

some kind of OR between the keys.. 键之间的某种OR

Thanks a lot 非常感谢

I once created a class for a similar purspose maybe it could help you. 我曾经为类似的目的创建了一个类,也许可以为您提供帮助。

template<typename T, typename K1, typename K2>
class BiKeyMap
{
public:
    typedef boost::shared_ptr<T> ptr_type;

private:
    std::map<K1, ptr_type > _map1;
    std::map<K2, ptr_type > _map2;
public:

    bool insert(const ptr_type& value, const K1& key1, const K2& key2)
    {
        bool lResult1;
        bool lResult2;
        lResult1= _map1.insert(std::make_pair(key1, value)).second;
        lResult2= _map2.insert(std::make_pair(key2, value)).second;
        return (lResult1&&lResult2);
    }

    ptr_type find1(const K1& key)
    {
        typename std::map<K1, ptr_type>::iterator itr = _map1.find(key);
        if (itr == _map1.end())
            throw ...;
        return itr->second;
    }

    ptr_type find2(const K2& key)
    {
        typename std::map<K2, ptr_type>::iterator itr = _map2.find(key);
        if (itr == _map2.end())
           throw ....
        return itr->second;
    }

};

The problem is in the way the operator< is implemented between std::pair (that use lexicographic order between components, so that an empty string will always be "first"). 问题在于在std :: pair之间实现了operator<的方式(在组件之间使用字典顺序,因此空字符串将始终是“ first”)。

Apart doing bad things like specialize operator< for std::pair<string,string> (that makes the new behavior available to any of such pairs even not involved in the map), you'll probably need as a key a class holding two strings, implementing operator< so that if one of the first members is empty the comparison is done only on the seconds. 除了做诸如std::pair<string,string> specialize operator<类的坏事情(这使新行为可用于任何这样的对,甚至不包含在映射中)之外,您可能还需要一个包含两个的类作为键。字符串,实现operator<以便如果第一个成员中的一个为空,则仅在几秒钟内进行比较。

like 喜欢

struct mykey { std::string fisrt, std::string second; };

bool operator<(const mykey& a, const mykey& b)
{ 
    int ck = (a.first.size() && b.first.size()) + 2*(a.second.size() && b.second.size());
    return (ck==1)? a.first<b.first:
           (ck==2)? a.second<b.second:
           a.first+a.second < b.first+b.second; 
}

and providing a ctor like 并提供像

mykey::mykey(const std::string s1, const std::string& s2) , so that you can create e "key to compare" by giving an empty string to s1 or s2, or insterting value in the map by giving both s1 and s2. mykey::mykey(const std::string s1, const std::string& s2) ,这样您就可以通过为s1或s2提供一个空字符串来创建e“要比较的键”,或者通过在两者中同时赋予值来在地图中插入值s1和s2。

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

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