struct re_use_dist
{
uint32_t set;
uint32_t way;
uint32_t rdx;
}reusedist;
map<pair<int, int>, int> myMap;
myMap[make_pair(reusedist.set,reusedist.way)] = reusedist.rdx;
我以struct的形式從文件中讀取數據並添加到地圖中。在地圖中添加多個元素<pair> C++
有沒有辦法將多個rdx添加到myMap?這種方式只存儲最後一個。
最後,我想借此連續rdx
差異爲每set
和way
謝謝!
int difference = 0;
int test = 0;
int primary = 0;
int secondary = 0;
float average = 0;
vector<int> rd;
for(multimap<pair<int, int>,int>::iterator st=myMap.begin(), end=myMap.end();st!=end;st++)
{
std::cout<<st->first.first<<" "<< st->first.second<<" "<<st->second<<endl;
if(primary != st->first.first && secondary != st->first.second){
vector<int>::size_type taille = rd.size();
double sum = 0;
for(vector<int>::const_iterator i = rd.begin(); i!=rd.end(); ++i)
{
sum+=*i;
}
average = sum/taille;
cout<<primary<<" "<<secondary<<" "<<average<<endl;
primary = st->first.first;
secondary = st->first.second;
exit(0);
}
difference = st->second - test;
rd.push_back(difference);
// cout<<difference<<endl;
test = st->second;
primary = st->first.first;
secondary = st->first.second;
cout<<primary<<" "<<secondary<<endl;
}
如果要爲同一個鍵存儲多個值,請使用'std :: multimap'。 – nakiya
正好你可以使用'std :: multimap',如果密鑰相同。 – pitnal