【问题标题】:iterating getters of a vector of pointers迭代指针向量的 getter
【发布时间】:2010-08-01 10:06:17
【问题描述】:

我正在尝试编写一个迭代器类,它在取消引用时返回一个 getter 函数返回值。代码工作正常,我唯一的问题是我想只使用一个模板参数而不是 3 来编写 member_ptr_functor,因为我应该能够从成员函数类型中推断出参数的类型和返回值。我认为有问题的是参数类型,我尝试使用 boost::remove_ptr&,但无法编译。

#include <iostream>
#include <vector>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/iterator/indirect_iterator.hpp>
using namespace boost;
using namespace std;

class CTest
{
private:
    int m_x;

public:
    CTest(int x) : m_x(x)
    {
    }

    const int& GetX() const
    {
        return m_x;
    }

};

template<typename MemFunType, typename ArgumentType, typename ResultType>
class member_ptr_functor : public unary_function<ArgumentType, ResultType>
{
private:
    MemFunType m_MemFun;

public:
    typedef ArgumentType argument_type;
    typedef ResultType result_type;

    member_ptr_functor(MemFunType MemFun) : m_MemFun(MemFun)
    {
    }

    result_type operator() (argument_type arg) const
    {
        return m_MemFun(&arg);
    }
};

template<typename MemFunType, typename ArgumentType, typename ResultType>
member_ptr_functor<MemFunType, ArgumentType, ResultType> make_member_ptr_functor(MemFunType MemFun)
{
    return member_ptr_functor<MemFunType, ArgumentType, ResultType>(MemFun);
}

class CPrintFunctor : public unary_function<int, void>
{
public:
    void operator() (const int n) const
    {
        cout << n << endl;
    }
};

int main()
{
    typedef vector<CTest> Container_t;
    Container_t v;
    v.push_back(CTest(1));

    CPrintFunctor PF;
    Container_t::const_iterator itStart = v.begin();
    Container_t::const_iterator itEnd = v.end();

    typedef member_ptr_functor<const_mem_fun_t<const int&, CTest> , CTest, const int&> member_ptr_functor_t;
    member_ptr_functor_t MemberPtrFunctor =     member_ptr_functor_t(mem_fun(&CTest::GetX));

    typedef transform_iterator<member_ptr_functor_t, Container_t::const_iterator, const int&, const int> transform_iterator_t;
    transform_iterator_t itTransformStart = make_transform_iterator(itStart, MemberPtrFunctor);
    transform_iterator_t itTransformEnd = make_transform_iterator(itEnd, MemberPtrFunctor);

    for_each(itTransformStart, itTransformEnd, PF);

    return 0;
}

哈盖。

【问题讨论】:

    标签: c++ iterator boost-iterators


    【解决方案1】:

    这并不能直接回答您的问题,而是建议替代方法。

    您已经在使用 Boost,为什么不更进一步,使用 Boost.Range:

    #include <boost/functional.hpp>
    #include <boost/range.hpp>
    #include <boost/range/algorithm/for_each.hpp>
    #include <boost/range/adaptor/transformed.hpp>
    
    // ... 
    
    int main ()
    {
      // ...
      boost::range::for_each (
        boost::adaptors::transform (v, boost::mem_fun_ref (&CTest::GetX)),
        PF);
    }
    

    这使您可以编写所需的功能,而无需定义自己的函子,也无需使用过多的 typedef。

    【讨论】:

    • 感谢您的回答。我不熟悉 boost.range,它可能是一个很好的长期解决方案。但是,我提供的代码只是一个示例,实际上我的代码中的其他内容需要迭代器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多