車のブランドIDと関連する車のモデルで構成されるリストがあります。例:
1カローラ
1ヤリス
1マトリックス
2チェロキー
2リバティ
3CR-
V3CR-Z3
エレメント
3シビック
3パイロット
ここで、1 =トヨタ、2 =ジープ、3=ホンダです。車のブランドごとに車種のカーディナリティが異なることに注意してください。
車のブランドごとにランダムな車種を取得したいと思います。車のブランドごとに取得される車の数は、関連するモデルの総数と入力フロートパラメーター'nPercentage'によって異なります。(「nPercentage」パラメーターは、すべての異なる自動車ブランドで同じです)。たとえば、nPercentage = 0.5の場合、可能なランダム出力は次のようになります。
1カローラ
1マトリックス
2リバティ
3CR-Z3
シビック
3パイロット
キーが複製される可能性があるため、現在マルチマップクラスを使用しています。これまでのところ、重複していないキーを見つけて、関連する要素の数を数えることができました。誰かが車のブランドごとにランダムな車のモデルを取得する方法に光を当てることができますか?以下、私がこれまでに持っているコード。
//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
*/
...
}