Reputation: 2456
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;
I read the data from a file in the form of struct and add to the map.
Is there a way to add multiple rdx to myMap? this way it is only storing the last one.
Finally, i would like to take difference of consecutive rdx
for each set
and way
Thanks!
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;
}
Upvotes: 1
Views: 3455
Reputation: 23058
std::multimap<pair<int, int>, int> myMap;
myMap.insert(make_pair(make_pair(reusedist.set, reusedist.way), reusedist.rdx));
To find the difference of consecutive 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;
}
Another approach: 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;
}
}
Upvotes: 1