我正在尝试对不同的人群进行模拟,每个群体具有多种基因型。每个群体都有一个map (mOccurrences),其中key =基因型,value =具有该基因型的个体数量。这些种群应该迁移,当个体进入另一个生态位时,生态位图应该更新,以反映新种群的基因组分布。因此,一旦我让我的个体迁移,我会尝试更新mOccurences;现在一些传入的基因可以与种群中的当前基因相同,所以它们应该只被添加,而另一些可以是新的,所以它们应该在地图中创建。我试着迭代新来者的map,然后使用:
mOccurrences.New->first += New->second;
但由于某些原因,它只添加了不存在的基因型,对于两个群体共有的基因型,个体数量不会改变。
我也尝试了find方法和insert方法,但都没有用…
void Population::addOccurrences( std::map<Allele, size_t> immigrants ) {
std::cout<< "Size NICHE Initiale " << mSize << std::endl; //shows size before immigrants
mSize = 0; //number of individuals in the niche in total must be updates
std::map<Allele, size_t>::iterator New;
std::map<Allele, size_t>::iterator it;
std::cout << "Occurrences initial" << std::endl; //PRINTINT current distribution to check
for(it = mOccurrences.begin(); it!= mOccurrences.end(); ++it) {
std::cout << it->first << '\t' << it->second << std::endl;
}
std::cout << "IMMIGRANT" << std::endl;
for(New = immigrants.begin(), it = mOccurrences.begin(); New != immigrants.end(); ++New, ++it)
{
std::cout << New->first << '\t' << New->second << std::endl; //PRINTING Immigrants genotype distribution
it = mOccurrences.find(New->first);
if(it != mOccurrences.end()) {
it->second += New->second; //if the allele is already present in population, only add individuals
} else {
mOccurences.insert(New); //else, insert the pair
}
}
std::cout << "Occurrences final" << std::endl; //PRINTING distribution after migration
for(it = mOccurrences.begin(); it!= mOccurrences.end(); ++it) {
mSize+= it->second;
std::cout << it->first << '\t' << it->second << std::endl;
}
}https://stackoverflow.com/questions/47796661
复制相似问题