首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++将映射复制到具有相同和/或不同密钥的另一个映射中

C++将映射复制到具有相同和/或不同密钥的另一个映射中
EN

Stack Overflow用户
提问于 2017-12-13 23:25:15
回答 0查看 706关注 0票数 0

我正在尝试对不同的人群进行模拟,每个群体具有多种基因型。每个群体都有一个map (mOccurrences),其中key =基因型,value =具有该基因型的个体数量。这些种群应该迁移,当个体进入另一个生态位时,生态位图应该更新,以反映新种群的基因组分布。因此,一旦我让我的个体迁移,我会尝试更新mOccurences;现在一些传入的基因可以与种群中的当前基因相同,所以它们应该只被添加,而另一些可以是新的,所以它们应该在地图中创建。我试着迭代新来者的map,然后使用:

mOccurrences.New->first += New->second;

但由于某些原因,它只添加了不存在的基因型,对于两个群体共有的基因型,个体数量不会改变。

我也尝试了find方法和insert方法,但都没有用…

代码语言:javascript
复制
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; 

}

}
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47796661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档