I've a map map<set,vector> m1
. It has values as follows:
< <1>,<2,4> >
< <2>,<6,2> >
< <3>,<3,4> >
< <4>,<6,1> >
< <5>,<1,1> >
Now I have to find the maximum values in eac开发者_运维问答h column of the vector and I do that easily by iterating all the rows and I store it in a vector say v1
as <6,4>
.
Now the problem is I want to find all the pairs that constitute to this value. Think of it like what are all the possible combination that can produce <6,4>
in the map. i.e. my result should also be a map that looks something like this :
< <1,2>,<6,4> >
< <2,3>,<6,4> >
< <1,4>,<6,4> >
< <3,4>,<6,4> >
EDIT:
To explain more, let the set in the map act as an id
of the corresponding vector. Now, what are all the vectors in that map "combined" can produce a <6,4> ? Note that the aggregate function here is the max
. i.e given to vectors <2,4>
and <6,2>
, the max between them is <6,4>
so id's (1 and 2) and (2 and 3) and so on can give me <6,4>.
What I was trying to do is iterate through every column of the vector in m1 and store the corresponding set
values whenever I find a 6
, in this example < <2> <6,2> >
and < <4>,<6,1> >
and do the same for the second column. Now I do not know how to integrate it to get my result.
#define SI map<set,vector>::iterator
mx = find_max();
vector<set> ret;
for(SI it=m1.begin();it!=m1.end();it++){
for(SI it2=it;it2=m1.end();it++){
if(it==it2)continue;//nasty
if(find_max2(it,it2)==mx)
ret.push_back(it->first,it2->first);
}
}
I don't know if this have compilation errors, but you can get the point.
#define SI map<set,vector>::iterator
vector<SI> ret;
for(SI it=m1.begin();it!=m1.end();it++){
if(equal(v1.begin(),v1.end(),it->second.begin()){
ret.push_back(it);
}
}
use std::equal to compare two vector.
精彩评论