P
Paul MG
Hi
Is there a simple general way of getting the keys out of a map? Or a vector of
pairs?
My problem seems to be that the pair<> type returned by iterating through either
has no methods to bind to to get the .first or .second elements. That seems to
me to be a major oversight - so I am assuming I must be making one!
For instance, imagine I want to use eg accumulate, to add up all the keys in a
map:
template<class A, class B>
struct FirstOfPair : public unary_function<const pair<A, B>, A> {
A operator()(const pair<A, B>& p) { return p.first; }
};
template<class A, class B>
struct AccumulatePairFirsts : public binary_function<A, const pair<A, B>, A> {
A operator()(A a, const pair<A, B>& p) { return a + FirstOfPair<A,B>()(p); }
};
int sumKeys(const map<int, string>& m) {
return accumulate(m.begin(), m.end(), 0,
AccumulatePairFirsts<int, string>());
}
This is pretty horrendous! It took me about 20 goes to get this all right. Also
I am not happy that I had to define AccumulatePairFirsts - couldnt I bind (or
compose?) with plus<int>() here?
Calling any gurus, please help!
I have a not-totally-weak functional background btw; I just often find it hard
to see how to do simple things using C++'s 'way'.
Cheers
Paul.
Is there a simple general way of getting the keys out of a map? Or a vector of
pairs?
My problem seems to be that the pair<> type returned by iterating through either
has no methods to bind to to get the .first or .second elements. That seems to
me to be a major oversight - so I am assuming I must be making one!
For instance, imagine I want to use eg accumulate, to add up all the keys in a
map:
template<class A, class B>
struct FirstOfPair : public unary_function<const pair<A, B>, A> {
A operator()(const pair<A, B>& p) { return p.first; }
};
template<class A, class B>
struct AccumulatePairFirsts : public binary_function<A, const pair<A, B>, A> {
A operator()(A a, const pair<A, B>& p) { return a + FirstOfPair<A,B>()(p); }
};
int sumKeys(const map<int, string>& m) {
return accumulate(m.begin(), m.end(), 0,
AccumulatePairFirsts<int, string>());
}
This is pretty horrendous! It took me about 20 goes to get this all right. Also
I am not happy that I had to define AccumulatePairFirsts - couldnt I bind (or
compose?) with plus<int>() here?
Calling any gurus, please help!
I have a not-totally-weak functional background btw; I just often find it hard
to see how to do simple things using C++'s 'way'.
Cheers
Paul.