多图擦除问题 [英] Multimap erase issue

查看:38
本文介绍了多图擦除问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将通过示例解释我的问题:

我正在声明多图这样的

 typedef struct _sAppTime 
{
tstring strProcessID;
bool bNotChanged;
} sAppTime;

typedef multimap< tstring,sAppTime> MMAPPTIME;
typedef MMAPPTIME :: iterator ITMMAPPTIME;





现在我必须删除那些有bool bNotChanged == true的值。满足此要求的代码如下所示。

 for(itMMAppTime = mmAppTime.begin(); itMMAppTime!= mmAppTime.end(); itMMAppTime ++)
{
//你必须这样做因为迭代器无效
ITMMAPPTIME erase_iter = itMMAppTime ++;
if(erase_iter-> second.bNotChanged)
{
mmAppTime.erase(erase_iter);
}
休息;
}





我通过插入一个值检查并检查。发生崩溃是因为itMMAppTime在擦除后没有任何有效指针。

请帮帮我...

提前谢谢

解决方案

您的代码有些问题:

1.为什么在第一个结束时有中断; for循环的迭代?

2.如果我没记错,所有迭代器都被擦除无效,而不仅仅是被删除的1。

3.没有休息如果一个元素被删除,你会双倍递增迭代器



我发现这样做的最好方法是

 for(itMMAppTime = mmAppTime.begin(); itMMAppTime!= mmAppTime.end(); / *此处没有* /){
if(erase_iter-> second.bNotChanged){
itMMAppTime = mmAppTime.erase(itMMAppTime);
} else {
++ itMMAppTime; //这里是for循环的最后一部分所以我们不增加两次
}
//为什么在这里有一个休息?
}


实际上这段代码

  #include   <   map  >  
#include < iostream >
使用 命名空间标准;
struct A
{
int a;
bool erase;
A( int a, bool erase):a(a),erase(erase) {}
};
int main()
{
multimap< string,A> MMAP;
mmap.insert(make_pair< string,A>(string( a1), A( 1 false )));
mmap.insert(make_pair< string,A>(string( a2), A( 2 true )));
mmap.insert(make_pair< string,A>(string( a3), A( 3 true )));
mmap.insert(make_pair< string,A>(string( a4), A( 4 false )));
for (multimap< string,A> :: iterator it = mmap.begin(); it!= mmap.end(); it ++)
{
if (it-> second.erase)mmap.erase(it);
}
for (multimap< string,A> :: iterator it = mmap.begin(); it!= mmap.end() ;它++)
{
cout<<它 - >第一<< <<它 - > second.a<< << it-> second.erase<< ENDL;
}
}





对我来说很好。

:)


I will explain my problem with example:
I am declaring multimap like that

typedef struct _sAppTime
{
    tstring strProcessID;
    bool bNotChanged;
}sAppTime;

typedef multimap<tstring,sAppTime> MMAPPTIME;
typedef MMAPPTIME::iterator ITMMAPPTIME;



Now I have to erase those value that have bool bNotChanged == true. The code fulfill this requirement is as shown below.

for (itMMAppTime =mmAppTime.begin(); itMMAppTime != mmAppTime.end();itMMAppTime++)
    {
    // you have to do this because iterators are invalidated
        ITMMAPPTIME erase_iter = itMMAppTime++;
       if(erase_iter->second.bNotChanged)
        {
                mmAppTime.erase(erase_iter);
        }
        break;
    }



I check this with insert one value and check. Crash occur because itMMAppTime do not have any valid pointer after erase.
Please help me out ...
Thanks in advance

解决方案

A few things wrong with your code:
1. Why do you have a break; at the end of the first iteration of the for loop?
2. If I recall correctly ALL iterators are invalidated by an erase, not just the 1 that is getting deleted.
3. Without the break, you would be double incrementing the iterator if an element was deleted

The best way that I have found to do this is

for (itMMAppTime = mmAppTime.begin(); itMMAppTime != mmAppTime.end(); /*nothing here*/) {
	if (erase_iter->second.bNotChanged) {
		itMMAppTime = mmAppTime.erase(itMMAppTime);
	} else {
		++itMMAppTime; //here is the last part of the for loop so we dont increment twice
	}
	//Why was there a break here?
}


Actually this piece of code

#include <map>
#include <iostream>
using namespace std;
struct A
{
  int a;
  bool erase;
  A(int a, bool erase):a(a),erase(erase){}
};
int main()
{
  multimap<string, A> mmap;
  mmap.insert(make_pair<string,A>(string("a1"), A(1,false)));
  mmap.insert(make_pair<string,A>(string("a2"), A(2,true)));
  mmap.insert(make_pair<string,A>(string("a3"), A(3,true)));
  mmap.insert(make_pair<string,A>(string("a4"), A(4,false)));
  for (multimap<string,A>::iterator it = mmap.begin(); it != mmap.end(); it++)
  {
    if (it->second.erase) mmap.erase(it);
  }
  for (multimap<string,A>::iterator it = mmap.begin(); it != mmap.end(); it++)
  {
    cout << it->first << ", " << it->second.a << ", " << it->second.erase << endl;
  }
}



works fine for me.
:)


这篇关于多图擦除问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆