【问题标题】:How to find the keys of map which are not in the keys of another map?如何找到不在另一个地图的键中的地图键?
【发布时间】:2014-10-15 06:41:11
【问题描述】:

我已经声明了两张地图m1m2

映射 m1 的键在 m2 的键中。但是 m2 的所有键都不在 m1 的键中。

谁能帮我找到 m2 中与 m1 的键相比不常见的键?

例子

m1 包含:

3=> 1  2  4
6=> 3  4 6

平方米包含:

3 =>  3  5  6
6 =>  6  4  8
8 =>  2  4  3
10 => 2  5  7  9

输出将是 8 和 10。

【问题讨论】:

    标签: c++ map


    【解决方案1】:

    您可以通过std::set_difference 进行操作。示例:

    std::map<int, std::string> m1;
    m1[3] = "1 2 4";
    m1[6] = "3 4 6";
    std::map<int, std::string> m2;
    m2[3] = "3 5 6";
    m2[6] = "6 4 8";
    m2[8] = "2 4 3";
    m2[10] = "2 5 7 9";
    
    std::map<int, std::string> m3;
    std::set_difference(m2.begin(), m2.end(), m1.begin(), m1.end(), std::inserter(m3, m3.begin()), m1.value_comp());
    
    for (auto i = m3.begin(); i != m3.end(); ++i) {
        std::cout << "[" << i->first << "," << i->second << "]";
    }
    std::cout << std::endl;
    

    结果:

    [8,2 4 3][10,2 5 7 9]
    

    LIVE

    【讨论】:

    • +1:我也有这个解决方案,但拒绝发布它,因为 OP 明确要求提供 keys (假设您的地图的第二个条目中有一些巨大的对象) .您能否以某种方式更改您的代码,使其将密钥插入到std::vector&lt;int&gt; 中?
    • 这里是只允许使用矢量键的成分:http://stackoverflow.com/a/16527081/2412846
    • @davidhigh 为了实现这一点,我们需要在 map 上的 key 迭代器,使用 Boost 的transform_iterator,或者自己实现。无论如何,这取决于OP。 :)
    【解决方案2】:

    这是一个通过定义一个新的key_iterator 来处理键的解决方案,它只返回键元素。该解决方案受到this post 的启发。

    #include <iostream>
    #include<algorithm>
    #include<map>
    #include<string>
    
    using namespace std;
    
    typedef std::map<int,std::string> MapType;
    typedef MapType::iterator IteratorType;
    
    struct key_iterator : public IteratorType
    {
        key_iterator() : IteratorType() {}
        key_iterator(IteratorType it) : IteratorType(it) {}
        int* operator->() {return (int* const)& IteratorType::operator->()->first;}
        int operator*()  {return IteratorType::operator*().first;}
    };
    
    int main() {
    
    std::map<int,std::string> m1;
    m1[3]="1 2 4";
    m1[6]="3 4 6";
    
    std::map<int,std::string> m2;
    m2[3]="3 5 6";
    m2[6]="6 4 8";
    m2[8]="2 4 3";
    m2[10]="2 5 7 9";
    
    std::vector<int> v;
    
    key_iterator it1_begin=m1.begin();
    key_iterator it1_end=m1.end();
    key_iterator it2_begin=m2.begin();
    key_iterator it2_end=m2.end();
    
    std::set_difference(it2_begin, it2_end, it1_begin, it1_end, std::inserter(v,v.begin()));
    
    for(auto i : v)
        std::cout<<i<<"  ";
    std::cout<<std::endl;
    
        // your code goes here
        return 0;
    }
    

    此代码打印

    8 10
    

    Live example.

    如果有人想出更好的语法来调用std::set_difference,请继续。

    【讨论】:

      【解决方案3】:

      您没有指定编程语言,所以这里有一些伪代码:

      m2.keySet() - m1.keySet()
      

      像 Python 这样的语言有一个 - 操作符可以在集合上工作,所以以上就是所有需要的。

      这是一些实际的 Python 代码:

      >>> m1 = {'x':4, 'y':3}
      >>> m2 = {'x':4, 'y':3, 'z':5}
      >>> set(m2.keys())-set(m1.keys())
      set(['z'])
      

      【讨论】:

      • 谢谢,其实我想用C++写代码,你能帮我用C++写代码吗?因为我不熟悉 Python。
      • 当然,同样的技术也适用。您可能有两个 std::map 类的对象。使用this StackOverflow question 中描述的技术将键提取到集合中,然后您可以使用std::set_difference 减去集合。
      • 我们可以在不使用类对象的情况下使用这种技术吗?我是 C++ 新手。
      • 我知道制作(真实)地图的唯一方法是使用std::map 的实例。如果你不使用这个类,你是如何表达地图的?作为结构体?
      猜你喜欢
      • 2020-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 2021-12-27
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多