Hi i am a beginner programmer in c++. I have a segmentation fault when i tried to run this program.
This below is where i think the problem lies, but i don't get why i get a segmentation fault. I tried running in repl.it it works perfectly but when i run in on my local MacOs compiled with g++ or clang++, i get a segmentation fault. Can anyone tell me why this happens and how can I debug it?
Suspect:
for (auto x: subset)
{
subset.erase(x);
canSum = canSum | sum(subset, n - x);
subset.insert(x);
}
Full code:
bool sum(std::set<int>& subset, int n)
{
if (n == 0)
{
return true;
}
else if (n < 0 || subset.empty())
{
return false;
}
bool canSum = 0;
if (!subset.empty())
{
for (auto x: subset)
{
subset.erase(x);
canSum = canSum | sum(subset, n - x);
subset.insert(x);
}
}
return canSum;
}
Aucun commentaire:
Enregistrer un commentaire