Re: member iterator - request for comment
Paul, thanks for the quick reply.
On 22 Cze, 01:35, Paul Bibbings <paul.bibbi...@gmail.com> wrote:
Mikosz <mikosz...@gmail.com> writes:
[...] to perform some action on one of the members of each of the
collection's elements. I couldn't find any solutions within the STL,
#include <vector>
#include <set>
#include <algorithm>
struct A {
A(int v) : val(v) { } // added for convenience here
int val;
};
struct val_functor {
int operator()(A a) { return a.val; }
};
int main()
{
std::vector<A> as;
std::set<int> vals;
for (int i = 0; i < 10; i++) as.push_back(i);
std::transform(as.begin(),
as.end(),
std::inserter(vals, vals.begin()),
val_functor());
}
Good point, I actually forgot about 'transform'. However, a solution
that would allow me not to implement an extracting functor would be
much neater. Again - I don't know of any existing solutions, so I
crafted my own. What do you think of this:
template<class PointerToMember, class Member>
class Extractor {
public:
Extractor(PointerToMember ptr) :
ptr_(ptr) {
}
template<class T>
Member& operator()(T& object) {
return object.*ptr_;
}
template<class T>
const Member& operator()(const T& object) const {
return object.*ptr_;
}
private:
PointerToMember ptr_;
};
template <class Member, class PointerToMember>
Extractor<PointerToMember, Member> makeExtractor(PointerToMember ptr)
{
return Extractor<PointerToMember, Member>(ptr);
}
struct A {
A(int v) :
val(v) {
}
int val;
};
int main() {
std::vector<A> as;
std::set<int> vals;
for (int i = 0; i < 10; i++) {
as.push_back(i);
}
std::transform(as.begin(), as.end(), std::inserter(vals,
vals.begin()), makeExtractor<int>(&A::val));
std::copy(vals.begin(), vals.end(),
std::ostream_iterator<int>(std::cout, ", "));
}
std::copy(makeMemberIterator<int>(as.begin(), &A::val),
makeMemberIterator<int>(as.end(), &A::val), std::back_inserter(vals));
and it works just fine.
I wouldn't have thought that the above would work at all. Given that
your vals is of type std::set<int>, I would have expected the above line
of code to fail on std::set not defining push_back.
Sure, that was a typing error. Should be std::inserter instead of
std::back_inserter in there.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]