Skip to content
This repository has been archived by the owner on Nov 13, 2021. It is now read-only.

i find a bug #10

Open
liangshaopeng opened this issue Nov 26, 2014 · 5 comments
Open

i find a bug #10

liangshaopeng opened this issue Nov 26, 2014 · 5 comments

Comments

@liangshaopeng
Copy link

helper.cpp

@liangshaopeng
Copy link
Author

extern void insert_element(multiset& m, multiset<double, std::greater >& M, double x){

if(m.empty() || x < *(m.begin()))
    M.insert(x);
else
    m.insert(x);
if(m.size() > M.size() + 1){
    multiset<double>::iterator i;
    i = m.begin();
    m.erase(m.begin());
    M.insert(*i);
}
else if(M.size() > m.size() + 1){
    multiset<double, std::greater<double> >::iterator i;
    i = M.begin();
    M.erase(M.begin());
    m.insert(*i);
}

}
i = M.begin();
M.erase(M.begin());
m.insert(*i)
this code here , after M.erase(M.begin()); iterator i is null;
same code also in remove_element() function.

@putnam120
Copy link
Contributor

Could you please provide the sample input that is causing you to get an error?

As it is written now the code checks to make sure that M is not empty. Thus you shouldn't get any error.

@liangshaopeng
Copy link
Author

any input will lead to this ,this is not about M is empty or not , this is c++ stl use fault . you can debug this function in gdb linux or dev c++ in windows,i have do it several times. thank you .

@liangshaopeng
Copy link
Author

i = M.begin();
M.erase(M.begin());
m.insert(_i);-------------------->_i is a random double , because i is Invalid after M.erase(M.begin());
i switch like this ,
i = M.begin();
m.insert(*i);
M.erase(M.begin());

@putnam120
Copy link
Contributor

Thanks, that slipped my mind. I'll make the correction.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants