Reputation: 1680
Having struct below:
struct MixingParts{
int intPart;
double doublePart;
}
and std::map as follow:
std::map<MixingParts, std::vector<int> > MixingMap;
I found boost::copy very useful, I will appreciate if you please help me to only extract integer part of struct as key of the above map and insert it back to the std::set intSet;
boost::copy(MixingMap | boost::adoptors::map_keys(.....*Is it possible to use bind here?*...), std::inserter(intSet, intSet.begin())
I can only use C++98, and also any other solution which is less verbose and optimized is appreciated.
Upvotes: 3
Views: 422
Reputation: 393134
boost::transform(mm, std::mem_fn(&MixingParts::intPart),
std::inserter(intSet, intSet.begin())));
Or use copy
/copy_range
with transformed
adaptor
Here's an integrated live example:
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <iostream>
struct MixingParts{
int intPart;
double doublePart;
};
using boost::adaptors::transformed;
int main() {
std::vector<MixingParts> v { { 1,0.1 }, {2, 0.2}, {3,0.3} };
boost::copy(v | transformed(std::mem_fn(&MixingParts::intPart)),
std::ostream_iterator<int>(std::cout, " "));
}
Prints
1 2 3
boost::mem_fn
can be used too.
Upvotes: 3
Reputation: 249293
for (MixingMap::const_iterator it = mm.begin(); it != mm.end(); ++it)
intSet.insert(it->first.intPart);
It would be less verbose in C++11, but it's hardly bloated by C++98 standards. And it's simple and optimally efficient (given the choice of data structures).
Upvotes: 1