我有这样的事情:
using namespace std;
class QuadraticPrimeSolution
{
private:
int a;
int b;
int numberOfPrimes;
bool isPrime(int n, set<int> &primeHash);
public:
QuadraticPrimeSolution(int a, int b):a(a),b(b),numberOfPrimes(0){};
void calculateNumberOfPrimes(set<int> &primeHash);
int getNumberOfPrimes(){return numberOfPrimes;}
};
class QuadraticPrimeSolver
{
private:
struct classcomp {
bool operator() (QuadraticPrimeSolution& lhs, QuadraticPrimeSolution& rhs)
{
return lhs.getNumberOfPrimes()>rhs.getNumberOfPrimes();
}
};
set<QuadraticPrimeSolution, classcomp> solutions;
set<int> primeHash;
QuadraticPrimeSolution getMaxSolution();
int a;
int b;
public:
QuadraticPrimeSolver(int a, int b):a(a), b(b){};
void solve();
};
bool QuadraticPrimeSolution::isPrime(int n, set<int> &primeHash)
{
if(primeHash.empty())
{
primeHash.insert(n);
return true;
}
for(auto it= primeHash.begin(); it!= primeHash.end(); it++)
{
if(n%(*it)==0)
{
return false;
}
}
primeHash.insert(n);
return true;
}
void QuadraticPrimeSolver::solve()
{
for(int i=(-1)*a; i<=a; i++)
{
for(int j=(-1)*b; j<=b; j++)
{
QuadraticPrimeSolution aSolution = new aSolution(i,j);
aSolution.calculateNumberOfPrimes(primeHash);
solutions.insert(aSolution);
}
}
}
int main()
{
QuadraticPrimeSolver QPS(0,40);
QPS.solve();
}
基本上我要做的是计算并将每个QuadraticPrimeSolution存储到QuadraticPrimeSolver中的哈希表中,然后我可以访问它。
我的问题是,我的比较器实现是否正确?现在编译器抱怨我的比较器,以及插入集合的以下行。
solutions.insert(aSolution);
请帮忙!
答案 0 :(得分:0)
class building
{
public:
int getPosition() const {return position;};
private:
int height;
int position;
};
class ManyBuildings
{
public:
void populateBuildings(std::vector<std::string> buildings);
private:
class comparePosition {
public:
bool operator () (const building &lhs, const building &rhs) {
return lhs.getPosition() > rhs.getPosition();
}
};
std::set<building, comparePosition> buildings;
};
set
代替unordered_set
set
的模板参数应该是类型,而不是函数