I'm trying to make a very simple filter using recursion but for some reason, I keep getting these Seg Fault.
#include <iostream>
#include <vector>
#include <math.h>
class FilterGeneric {
public:
std::vector<int> filter(std::vector<int>& v, std::vector<int>::iterator i); //set i=v.begin() in main
private:
virtual bool g(int x) =0;
std::vector<int> result;
};
std::vector<int> FilterGeneric::filter(std::vector<int>& v, std::vector<int>::iterator i) {
if (i==v.end()) {
return result;
} else {
if (g(*i)==true) {
result.push_back(*i);
i++;
return filter(v,i);
} else {
i++;
return filter(v,i);
}
}
}
class FilterOdd : public FilterGeneric {
private:
bool g(int x);
};
bool FilterOdd::g(int x) {
if ((x%2)!=0) {
return true;
} else {
return false;
}
}
class FilterNonPositive : public FilterGeneric {
private:
bool g(int x);
};
bool FilterNonPositive::g(int x) {
if (x<0) {
return true;
} else {
return false;
}
}
class FilterForTwoDigitPositive : public FilterGeneric {
private:
bool g(int x);
};
bool FilterForTwoDigitPositive::g(int x) {
if (x>=10) {
return true;
} else {
return false;
}
}
int main() {
std::vector<int> v;
std::vector<int>::iterator it=v.begin();
for (int i=0;i<20;i++) {
v.push_back(pow(-1,i)*i);
}
std::cout<<std::endl;
FilterNonPositive fnp;
FilterForTwoDigitPositive ftd;
FilterGeneric *f1=&fnp;
FilterGeneric *f2=&ftd;
std::vector<int> r1;
std::vector<int> r2;
r1=f1->filter(v,it);
for (it=r1.begin();it!=r1.end();it++) {
std::cout<<*it<<" ";
}
r2=f2->filter(v,it);
for (it=r2.begin();it!=r2.end();it++) {
std::cout<<*it<<" ";
}
}
std::stackand a simple loop instead.