organofjohannbach's blog

By organofjohannbach, 13 years ago, In English

declaration:
int nm=0; 
vector<pair<int,int> > tv;
set<pair<int,int> > iset;
int ans[200500][2]={};

Programme A:

for(set<pair<int,int> >::iterator p=iset.begin();cnn++;)
{
    if(p==iset.end())
    {
        puts("No");
        return 0;
    }   
    if((p->first)+1)
        tv.push_back(make_pair((p->first)+1,p->second));
    ++nm;
    ans[nm][0]=member;
    ans[nm][1] = p->second ;
    iset.erase(p++);    
}

this one got Accepted;

However Programme B:
for(set<pair<int,int> >::iterator p=iset.begin();cnn++;++p)
{
    if(p==iset.end())
    {
        puts("No");
        return 0;
    }   
    if((p->first)+1)
        tv.push_back(make_pair((p->first)+1,p->second));
    ++nm;
    ans[nm][0]=member;
    ans[nm][1] = p->second ;
    iset.erase(p);    
}

exit code: -1073741819 RUNTIME_ERROR

What confuse me most is both of two running correctly on my PC;here is some information of my PC:ubuntu 11.10 netbeans 7.0.1

I hope if someone could help me,thanks.

  • Vote: I like it
  • +6
  • Vote: I do not like it

»
13 years ago, # |
  Vote: I like it +15 Vote: I do not like it
iset.erase(p++); VS  iset.erase(p); ++p;
In the first case p will have value of its successor.
In the second - corrupted value, cause after erasing the iterator became invalid.