Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;检查两个贴图中是否存在对象_C++_Dictionary - Fatal编程技术网

C++ C++;检查两个贴图中是否存在对象

C++ C++;检查两个贴图中是否存在对象,c++,dictionary,C++,Dictionary,我有自己的目标 class my_object { int id; bool state; string name; string vendor; } 我想将我的对象存储到两个map中,以便快速参考 std::map<string, my_object> map1; std::map<string, my_object> map2; 如何在两个地图中完成比较工作? 或者我应该使用不同的数据结构 更新: 谢谢你的回复。 我之所以使用两个贴图是因为两个不同

我有自己的目标

class my_object
{
  int id;
  bool state;
  string name;
  string vendor;
}
我想将我的对象存储到两个
map
中,以便快速参考

std::map<string, my_object> map1;
std::map<string, my_object> map2;
如何在两个地图中完成比较工作? 或者我应该使用不同的数据结构

更新: 谢谢你的回复。 我之所以使用两个贴图是因为两个不同的函数将生成贴图:

function1() //returns map1;
function2() //returns map2;

这两个映射中使用的键是my_对象的
name
字段。对于“足够快的引用”,我认为如果map1有n个元素,map2有m个元素,那么我的计算时间是n*m吗?

您可以迭代其中一个映射(理想情况下,如果元素的数量可能会显著变化并且性能很重要,您可以选择较短的一个),并且在执行此操作时在另一个映射中依次搜索每个键(使用)

你可以

for (const auto& m1 : map1) {
    auto i2 = map2.find(m1.first);
    if (i2 != map2.end() && m1.second.vendor == i2->second.vendor) {
        // do some work
    }
}

这是一个程序,它将为两个映射中存在的每个键调用任意函数。它基于的示例实现

您可以修改lambda以执行
供应商
平等性测试或任何其他检查

 #include <map>
 #include <string>
 #include <iostream>

 template<typename K, typename V1, typename V2, typename Func>
 void map_intersection(std::map<K,V1> const &m1, std::map<K,V2> const &m2, Func f)
 {
    auto it1 = m1.begin(), it2 = m2.begin();

    while (it1 != m1.end() && it2 != m2.end() ) {
         if (it1->first < it2->first) {
             ++it1;
         } else  {
             if (!(it2->first < it1->first)) {
                 f(it1->second, it2->second);
             }
             ++it2;
         }
     }  
 }

 int main()
 {
     std::map<std::string, std::string> map1 = { {"a", "apple"}, {"b", "bug"}, {"c", "car"} };
     std::map<std::string, std::string> map2 = { {"b", "boat"}, {"c", "car"} };

     map_intersection(map1, map2, [](std::string const &v1, std::string const &v2)
     {
         std::cout << "Same key: " << v1 << "," << v2 << '\n';
     });
 }

这两张地图上的两个键是什么?一个地图由
名称
键入,另一个由
供应商
键入?必须有一个解决方案,但它会非常有效。如果你告诉我们你想做什么,这可能会有所帮助。如果两个地图中都有相同的键,这是否保证了相同的对象?@M.M,OP的情况似乎不是这样。我没有找到合乎逻辑的答案。虽然不是这个问题的直接答案,但如果你首先分析为什么你有两张地图,你可能会得到更好的服务——“为了足够快的参考”留下了一些有待改进的地方。结果可能是您并不真正需要它,或者使用不同的容器效果更好。
for (const auto& m1 : map1) {
    auto i2 = map2.find(m1.first);
    if (i2 != map2.end() && m1.second.vendor == i2->second.vendor) {
        // do some work
    }
}
 #include <map>
 #include <string>
 #include <iostream>

 template<typename K, typename V1, typename V2, typename Func>
 void map_intersection(std::map<K,V1> const &m1, std::map<K,V2> const &m2, Func f)
 {
    auto it1 = m1.begin(), it2 = m2.begin();

    while (it1 != m1.end() && it2 != m2.end() ) {
         if (it1->first < it2->first) {
             ++it1;
         } else  {
             if (!(it2->first < it1->first)) {
                 f(it1->second, it2->second);
             }
             ++it2;
         }
     }  
 }

 int main()
 {
     std::map<std::string, std::string> map1 = { {"a", "apple"}, {"b", "bug"}, {"c", "car"} };
     std::map<std::string, std::string> map2 = { {"b", "boat"}, {"c", "car"} };

     map_intersection(map1, map2, [](std::string const &v1, std::string const &v2)
     {
         std::cout << "Same key: " << v1 << "," << v2 << '\n';
     });
 }
Same key: bug,boat
Same key: car,car