成员变量和STL算法

发布于 2024-08-05 10:37:30 字数 586 浏览 2 评论 0原文

#include <vector>
#include <functional>
#include <algorithm>
using namespace std;

struct Foo
{
    int i;
    double d;
    Foo(int i, double d) :
        i(i),
        d(d)
    {}
    int getI() const { return i; }
};

int main()
{
    vector<Foo> v;
    v.push_back(Foo(1, 2.0));
    v.push_back(Foo(5, 3.0));

    vector<int> is;

    transform(v.begin(), v.end(), back_inserter(is), mem_fun_ref(&Foo::getI));

    return 0;
}

有没有一种更干净的方法来访问成员变量,然后使用像我上面这样的成员函数?我知道如何使用 tr1::bind 来做到这一点,但我需要有 C++03 兼容的代码而不需要 boost。

#include <vector>
#include <functional>
#include <algorithm>
using namespace std;

struct Foo
{
    int i;
    double d;
    Foo(int i, double d) :
        i(i),
        d(d)
    {}
    int getI() const { return i; }
};

int main()
{
    vector<Foo> v;
    v.push_back(Foo(1, 2.0));
    v.push_back(Foo(5, 3.0));

    vector<int> is;

    transform(v.begin(), v.end(), back_inserter(is), mem_fun_ref(&Foo::getI));

    return 0;
}

Is there a cleaner way to access a member variable then then using a member function like I have above? I know how to do it using tr1::bind, but I need to have C++03 compliant code without boost.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〆凄凉。 2024-08-12 10:37:30

需要访问器函数来做到这一点绝对是不干净的。但这就是当前的C++。

您可以尝试使用 boost::bind,它很容易实现这一点,或者使用 for( vector::const_iterator it = v.begin() 显式迭代向量; .....) 循环。我发现当函子的创建变得太麻烦时,后者通常会产生更清晰的代码。

或者,避开 boost,创建您自己的成员访问器填充函数。

template< typename T, typename m > struct accessor_t {
   typedef m (T::*memberptr);

   memberptr acc_;

   accessor_t( memberptr acc ): acc_(acc){}
   // accessor_t( m (T::*acc) ): acc_(acc){}
   // m (T::*acc_);

   const m& operator()( const T& t ) const { return (t.*acc_); }
   m&       operator()( T& t       ) const { return (t.*acc_); }
};

template< typename T, typename m > accessor_t<T,m> accessor( m T::*acc ) {
   return accessor_t<T,m>(acc);
}

...

transform( v.begin(), v.end(), back_inserter(is), accessor( &C::i ) );

It's absolutely unclean to need an accessor function in order to do that. But that's the current C++.

You could try using boost::bind, which does the trick quite easily, or iterate the vector explicitly, using a for( vector<int>::const_iterator it = v.begin(); .....) loop. I find the latter often resulting in clearer code when the creation of the functor becomes too much a hassle.

Or, shunning boost, create your own member-accessor shim function.

template< typename T, typename m > struct accessor_t {
   typedef m (T::*memberptr);

   memberptr acc_;

   accessor_t( memberptr acc ): acc_(acc){}
   // accessor_t( m (T::*acc) ): acc_(acc){}
   // m (T::*acc_);

   const m& operator()( const T& t ) const { return (t.*acc_); }
   m&       operator()( T& t       ) const { return (t.*acc_); }
};

template< typename T, typename m > accessor_t<T,m> accessor( m T::*acc ) {
   return accessor_t<T,m>(acc);
}

...

transform( v.begin(), v.end(), back_inserter(is), accessor( &C::i ) );
久光 2024-08-12 10:37:30

像 std::pair 一样,您可以编写访问或对象。

#include <vector>
#include <algorithm>

struct Foo
{
    int i;
    double d;
};

struct GetI { int    operator()(Foo const& o) const { return o.i;}};
struct GetD { double operator()(Foo const& o) const { return o.d;}};

int main()
{
    std::vector<Foo>    v;
    std::vector<int>    t;
    std::transform(v.begin(), v.end(), std::back_inserter(t),GetI() );
}

注意:您应该查看 std::pair
及其访问器: std::select1st和 std::select2nd

Like std::pair you could write access-or objects.

#include <vector>
#include <algorithm>

struct Foo
{
    int i;
    double d;
};

struct GetI { int    operator()(Foo const& o) const { return o.i;}};
struct GetD { double operator()(Foo const& o) const { return o.d;}};

int main()
{
    std::vector<Foo>    v;
    std::vector<int>    t;
    std::transform(v.begin(), v.end(), std::back_inserter(t),GetI() );
}

Note: You should look at std::pair<T1,T2>
And its accessors: std::select1st<T1> and std::select2nd<T2>

感受沵的脚步 2024-08-12 10:37:30

对我来说最清楚的方法是 boost::bind

#include <boost/bind.hpp>

...

transform(v.begin(), v.end(), back_inserter(is), bind( &Foo::i, _1 ) );

当然,您可以创建自己的成员访问函数,但我相信这会使您的代码可读性较差。 boost::bind 是众所周知的库,因此使用它将使您的代码具有良好的可读性,并且无需阅读辅助函数(偶尔可能包含错误)

我更喜欢的第二种方法是仅使用 for 循环(在这种特殊情况下 ) ):

for ( vector<Foo>::const_iterator it = v.begin(), it != v.end(); ++it )
    is.push_back( it->i );

也许使用如此简单的循环并不流行,但它们非常清晰。

The most clear way for me is boost::bind:

#include <boost/bind.hpp>

...

transform(v.begin(), v.end(), back_inserter(is), bind( &Foo::i, _1 ) );

Surely you could create your own member access function, but I believe it will make your code less readable. boost::bind is widely known library, so using it will make your code pretty readable and no need to read your helper functions (which could contain bugs occasionally)

The second way I prefer is just to use for-loop (in this particular case):

for ( vector<Foo>::const_iterator it = v.begin(), it != v.end(); ++it )
    is.push_back( it->i );

May be it is not fashionable to use such simple loops, but they are very clear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文