Блог пользователя organofjohannbach

Автор organofjohannbach, 12 лет назад, По-английски

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.

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

»
12 лет назад, # |
  Проголосовать: нравится +15 Проголосовать: не нравится
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.