【发布时间】:2015-07-01 19:09:34
【问题描述】:
我这几天一直在处理这个问题。我正在运行一个进化模型,该模型使用分布来进行物种之间的相互作用。我在这里粘贴了函数。我需要使用模板:
template <class InputIt>
如果我将模板直接粘贴在函数声明之前,函数将无法识别该模板。如果我在 main() 之前粘贴它,模板会被识别,但我得到一个错误:
错误 2 错误 LNK1120: 1 未解决的外部
代码:
void evolution(TeamArray& teamData)
{
default_random_engine randomGenerator((unsigned int)time(NULL));
uniform_int_distribution<int> rand120(0, 120);
int* RandA;
int* RandB;
RandA = new int[rounds];
RandB = new int[rounds];
// Time Period 1
for (int i = 0; i < rounds / 10; ++i)
{
RandA[i] = rand120(randomGenerator); // generates random numbers from 0 to 120 (121 elements)
}
for (int i = 0; i < rounds / 10; ++i)
{
RandB[i] = rand120(randomGenerator); // generates random numbers from 0 to 120 (121 elements)
}
for (int i = 0; i < rounds / 10; i++)
{
interact(teamData[RandA[i]], teamData[RandB[i]]);
}
delete[] RandA;
delete[] RandB;
// Later time periods (ERA 2 through 10)
// The inside of this loop does the rest of the interactions
for (int t = 1; t < 10; ++t) // looping through the ERA's
{
// Intializing distribution
std::vector< int> weights(121);
for (int i = 0; i < 121; i++)
{
weights[i] = (teamData[i]).S();
}
std::discrete_distribution<int&> dist(weights.begin(), weights.end());
for (int i = t* (rounds / 10); i < (t+1) * (rounds / 10); ++i)
{
RandA[i] = dist(randomGenerator);
}
for (int i = t* (rounds / 10); i < (t + 1) * (rounds / 10); ++i)
{
RandB[i] = dist(randomGenerator);
}
for (int i = t* (rounds / 10); i < (t + 1) * (rounds / 10); ++i)
{
interact(teamData[RandA[i]], teamData[RandB[i]]);
}
delete[] RandA;
delete[] RandB;
}
【问题讨论】:
-
constructor of
std::discrete_distribution只接受一个参数。你给了它两个参数。 -
好的。那么我将如何输入权重向量?我也试过这个:std::discrete_distribution
dist(weights); -
不幸的是,我不太了解随机数生成器的复杂性,无法提出解决方案。
标签: c++ distribution