模板类执行错误
下面是我的代码,用于根据用户定义的权重值生成随机数。这一切都有效,直到我试图让数据类型成为任何类型,例如双,浮动。我没有什么经验在实践中实施,只能在教科书上阅读。任何人都可以帮助我解决它吗?模板类执行错误
感谢,
class WeightedRandom
{
public:
template <class T1,class T2>
void setWeight(T1 i,T2 val)
{
m[i]=val;
total+=val;
}
void generator()
{
int val=rand()%total;
for (auto a:m)
{
if (val<a.second)
{
res[a.first]++;
break;
}
val-=a.second;
}
}
void print()
{
for (auto a:res)
{
cout<<a.first<<" "<<a.second<<endl;
}
}
private:
template <class T1,class T2>
unordered_map<T1,T2> m;
template <class T3,class T4>
unordered_map<T3,T4> res; // object-count
int total=0;
};
int main(int argc, const char * argv[])
{
WeightedRandom WR;
WR.setWeight(1, 5);
WR.setWeight(2, 20);
WR.setWeight(3, 50);
WR.setWeight(4, 20);
WR.setWeight(5, 10);
int n=10000;
for (int i=0;i<n;++i)
{
WR.generator();
}
WR.print();
}
你只需要到模板类,允许总成为模板类型。
#include<unordered_map>
#include<iostream>
#include<math.h>
using namespace std;
template<typename T1,typename T2>
class WeightedRandom{
public:
void setWeight(T1 i,T2 val)
{
m[i]=val;
total+=val;
}
void generator()
{
T2 val= (T2) fmod(rand(),total);
for (auto a:m)
{
if (val<a.second)
{
res[a.first]++;
break;
}
val-=a.second;
}
}
void print()
{
for (auto a:res)
{
cout<<a.first<<" "<<a.second<<endl;
}
}
private:
unordered_map<T1,T2> m;
unordered_map<T1,T2> res; // object-count
T2 total=0;
};
int main(int argc, const char * argv[])
{
WeightedRandom<int,double> WR;
WR.setWeight(1, 5.01);
WR.setWeight(2, 19.99);
WR.setWeight(3, 50.01);
WR.setWeight(4, 19.99);
WR.setWeight(5, 10.00);
int n=10000;
for (int i=0;i<n;++i)
{
WR.generator();
}
WR.print();
}
FMOD采取了一倍,所以如果它是一个整数或浮点数,它会被提升到双,结果将投退下来,或者如果它是一个双,投什么都不做。你可能需要考虑增加一些检查,以确保您可以只使用双/ float和char /短路/ INT /长,因为用户可能会使用一些类的权重,其将不会做出太大的意义:
...
class WeightedRandom{
static_assert(!is_same<T,bool>(),"type can't be a bool");
static_assert(is_arithmetic<T>(),"type needs to be an arithmetic");
...
谢谢,奥斯汀。真的很有帮助。 – landlord1984
没问题,虽然现在我想到了。你一定需要这些检查,因为FMOD将无法对非算术,所以如果你有WeightedRandom
你会得到一个神秘的错误消息,我想有一个错误的输入,如:WR.setWeight(真,“A”);但断言不会打印出来。看来WeightedRandom
什么是错误?顺便说一句,在'total + = val;''val'是T2的时候,你有'int total'。 – Serge
是原来的地方,现在所有的模板都是int吗? –