0
我有包括车名单分组元素的随机子集品牌标识的和相关的车型,如:从multimap中通过按键
1花冠
1威姿
1矩阵
2切诺基
2自由
3 CR-V
3 CR-Z
3元
3文娱
3先导
其中1 =丰田,2 =吉普,3 =本田。请注意,每个汽车品牌的汽车模型的基数不同。
我想检索每个汽车品牌的随机车型。每个汽车品牌要检索的汽车数量取决于相关车型的总数和输入浮动参数:'nPercentage'。 ('nPercentage'参数对于所有不同的汽车品牌都是相同的)。例如,如果nPercentage = 0.5,可能的随机的输出将是:
1花冠
1矩阵
2自由
3 CR-Z
3文娱
3先导
I” m目前正在使用multimap类,因为密钥可以被复制。到目前为止,我能够找到未复制的键并计算关联元素的数量。 任何人都可以点亮如何检索每个汽车品牌的随机车型吗? 下面,我到目前为止的代码。
//The variable 'm_mapDatasetMapping' is of type: multimap<int, string>
multimap< int, string >::size_type countPerKey;
const int *pLastKey = NULL;
multimap<int,string>::const_iterator it=m_mapDatasetMapping.begin();
// looking for non-duplicated keys.
for(; it!=m_mapDatasetMapping.end(); it++){
if((pLastKey!=NULL) && (*pLastKey==it->first)){
continue;
}
pLastKey = &(it->first);
// count the number of values associated to the given key.
countPerKey = m_mapDatasetMapping.count(*pLastKey);
/* Select 'x' random elements associated with the key '*pLastKey'.
The number of random elements to be extracted
is a percentage of the total number of values per key, i.e.:
x = nPercentage * countPerKey
*/
...
}
谢谢!它像一个魅力!我只需要改变变量'range'的'type',即从'auto'变为'pair :: iterator,multimap :: iterator>',并且变量'i'的类型也从' auto'到'multimap :: iterator'。 –
Javier
2011-03-23 14:14:18
@Javier for non-selected,你是不是指如何使用'values [x]'中的值初始化一个新的向量到values.end()? 'vector other_values(values.begin()+ x,values.end());',在'values.resize(x)'之前。是的,'auto'类型可以节省大量现代编译器的编写工作! –
Cubbi
2011-03-23 14:14:53
是的!这是我做到的。 – Javier 2011-03-23 14:16:53