Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.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++_C++11_Map_Intersection - Fatal编程技术网

C++;在关键点上相交两个贴图,保留第一个贴图的值 我遇到了C++和地图和交叉点的问题。

C++;在关键点上相交两个贴图,保留第一个贴图的值 我遇到了C++和地图和交叉点的问题。,c++,c++11,map,intersection,C++,C++11,Map,Intersection,有3张地图,前两张是map,最后一张是map。 我想从前两个贴图中删除所有贴图键实例,这些实例在第三个贴图中不作为键存在。简言之,我有第三个贴图,其中包含对象列表,前两个贴图包含有关对象的一些数据。在某个时间点,带有对象的地图被清理,一些项目被删除(用户交互),现在需要分别清理其他两个地图 我尝试了以下方法: map<int, double> map1, map2; map<int, CustomType> map3; for (auto it = map1.cbegin

有3张地图,前两张是
map
,最后一张是
map
。 我想从前两个贴图中删除所有贴图键实例,这些实例在第三个贴图中不作为键存在。简言之,我有第三个贴图,其中包含对象列表,前两个贴图包含有关对象的一些数据。在某个时间点,带有对象的地图被清理,一些项目被删除(用户交互),现在需要分别清理其他两个地图

我尝试了以下方法:

map<int, double> map1, map2;
map<int, CustomType> map3;
for (auto it = map1.cbegin(); it != map1.cend(); )
{
    if ( map3.find(it->first) == map3.end() )
    {
        map2.erase(it);
        map1.erase(it++);
    }
    else ++it;
}
map1、map2;
地图map3;
for(auto it=map1.cbegin();it!=map1.cend();)
{
if(map3.find(it->first)=map3.end()
{
map2.删除(它);
map1.擦除(it++);
}
否则它;
}
这在map1.erase行上显示了一个错误“未分配要释放的指针”。我已经研究过set_交叉点,但我不相信它在这种情况下会起作用,因为值会不同


非常感谢您的帮助。

您已经接近解决方案,但问题是迭代器实质上就是一个指针。
因此,您可以使用“it”在map1和map2上同时删除

void removeUnexist(const map<int, double>& m, const map<int, CustomType>::iterator& it) {
   i = m.find(it->first);
   if(i == m.end()) {
      m.erase(i);
   }
}

map<int, double> map1, map2;
map<int, CustomType> map3;

for (auto it = map3.cbegin(); it != map3.cend(); it++) {
   removeUnexist(map1, it);
   removeUnexist(map2, it);
}
void removenulist(常量映射&m,常量映射::迭代器&it){
i=m.find(it->first);
如果(i==m.end()){
m、 删除(i);
}
}
map1,map2;
地图map3;
for(auto it=map3.cbegin();it!=map3.cend();it++){
RemoveUnList(地图1,it);
RemoveUnList(地图2,it);
}

您已经接近解决方案,但问题是迭代器实质上就是一个指针。
因此,您可以使用“it”在map1和map2上同时删除

void removeUnexist(const map<int, double>& m, const map<int, CustomType>::iterator& it) {
   i = m.find(it->first);
   if(i == m.end()) {
      m.erase(i);
   }
}

map<int, double> map1, map2;
map<int, CustomType> map3;

for (auto it = map3.cbegin(); it != map3.cend(); it++) {
   removeUnexist(map1, it);
   removeUnexist(map2, it);
}
void removenulist(常量映射&m,常量映射::迭代器&it){
i=m.find(it->first);
如果(i==m.end()){
m、 删除(i);
}
}
map1,map2;
地图map3;
for(auto it=map3.cbegin();it!=map3.cend();it++){
RemoveUnList(地图1,it);
RemoveUnList(地图2,it);
}

您需要独立地迭代
map1
map2
。您不能使用
map1
的迭代器操作另一个映射(从
map2
删除
,或使用
map2
执行任何其他操作)。
所以代码应该是这样的:

map<int, double> map1, map2;
map<int, CustomType> map3;
for (auto it = map1.cbegin(); it != map1.cend(); )
{
    if ( map3.find(it->first) == map3.end() )
        it = map1.erase(it);
    else
        ++it;
}

for (auto it = map2.cbegin(); it != map2.cend(); )
{
    if ( map3.find(it->first) == map3.end() )
        it = map2.erase(it);
    else
        ++it;
}
map1、map2;
地图map3;
for(auto it=map1.cbegin();it!=map1.cend();)
{
if(map3.find(it->first)=map3.end()
它=映射1。擦除(它);
其他的
++它;
}
for(auto it=map2.cbegin();it!=map2.cend();)
{
if(map3.find(it->first)=map3.end()
它=映射2。擦除(它);
其他的
++它;
}

您需要独立地迭代
map1
map2
。您不能使用
map1
的迭代器操作另一个映射(从
map2
删除
,或使用
map2
执行任何其他操作)。
所以代码应该是这样的:

map<int, double> map1, map2;
map<int, CustomType> map3;
for (auto it = map1.cbegin(); it != map1.cend(); )
{
    if ( map3.find(it->first) == map3.end() )
        it = map1.erase(it);
    else
        ++it;
}

for (auto it = map2.cbegin(); it != map2.cend(); )
{
    if ( map3.find(it->first) == map3.end() )
        it = map2.erase(it);
    else
        ++it;
}
map1、map2;
地图map3;
for(auto it=map1.cbegin();it!=map1.cend();)
{
if(map3.find(it->first)=map3.end()
它=映射1。擦除(它);
其他的
++它;
}
for(auto it=map2.cbegin();it!=map2.cend();)
{
if(map3.find(it->first)=map3.end()
它=映射2。擦除(它);
其他的
++它;
}

您正试图使用map1中的迭代器从map2中删除元素。那不行。您需要从迭代器中获取键值,并使用该键值从map2中删除。当您为map1调用erase时,您使迭代器无效,因为您删除了它所指向的元素。递增迭代器,然后使用键值调用map1.erase()。

您正试图使用map1中的迭代器从map2中删除元素。那不行。您需要从迭代器中获取键值,并使用该键值从map2中删除。当您为map1调用erase时,您使迭代器无效,因为您删除了它所指向的元素。递增迭代器,然后使用键值调用map1.erase()。

。。。但是当你做
autonext=++it
,您不小心跳过了map1/2中的第一个元素,现在可能位于
end()
元素上:如果是
中的
it->first
,则在
中使用
it
可能会崩溃并烧掉。接下来为什么?完全没有用,而且dangerous@Velthune:在C++03中,在使用erase之前,必须将迭代器存储到下一个元素,因为您删除的迭代器随后无效,并且无法递增。在C++11中更简单。你的答案是有效的(和下面的其他人说的一样)。问题是在map2上使用map1的迭代器。将代码加倍可以很好地工作。同意不需要next变量。@Velthune,好的,我重写了next而没有
next
。但是为什么你认为下一个
是危险的呢?几乎。。。但是当你做
autonext=++it
,您不小心跳过了map1/2中的第一个元素,现在可能位于
end()
元素上:如果是
中的
it->first
,则在
中使用
it
可能会崩溃并烧掉。接下来为什么?完全没有用,而且dangerous@Velthune:在C++03中,在使用erase之前,必须将迭代器存储到下一个元素,因为您删除的迭代器随后无效,并且无法递增。在C++11中更简单。你的答案是有效的(和下面的其他人说的一样)。问题是在map2上使用map1的迭代器。将代码加倍可以很好地工作。同意不需要next变量。@Velthune,好的,我重写了next而没有
next
。但是为什么您认为下一个<代码>很危险?问题是“从前两个地图中删除所有地图键实例,它们在第三个地图中不作为键存在”,但您正在从前两个地图的第三个地图中删除任何键。。。完全不同。是的,正如你所说的“擦除被称为o”