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的形式从文件中读取数据并添加到地图中。
有没有办法将多个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;
}
答案 0 :(得分:1)
std::multimap<pair<int, int>, int> myMap;
myMap.insert(make_pair(make_pair(reusedist.set, reusedist.way), reusedist.rdx));
查找连续rdx
:
for (std::multimap<pair<int, int>, int>::const_iterator i = myMap.begin(); i != myMap.end(); ) {
pair<int, int> key = i->first;
std::multimap<pair<int, int>, int>::const_iterator rbound = myMap.upper_bound(key);
std::multimap<pair<int, int>, int>::const_iterator j = i, k = i;
++k;
for (; k != rbound; j = k, ++k) {
int difference = k->second - j->second;
}
i = rbound;
}
另一种方法:map<pair<int, int>, vector<int> >
。
map<pair<int, int>, vector<int> > myMap;
myMap[make_pair(reusedist.set, reusedist.way)].push_back(reusedist.rdx);
for (map<pair<int, int>, vector<int> >::const_iterator i = myMap.begin(); i != myMap.end(); ++i) {
pair<int, int> key = i->first;
const vector<int> &rdxes = i->second;
for (vector<int>::const_iterator j = rdxes.begin(); j+1 != rdxes.end(); ++j) {
int difference = *(j+1) - *j;
}
}