成员变量和STL算法
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
需要访问器函数来做到这一点绝对是不干净的。但这就是当前的C++。
您可以尝试使用
boost::bind
,它很容易实现这一点,或者使用for( vector::const_iterator it = v.begin() 显式迭代向量; .....) 循环。我发现当函子的创建变得太麻烦时,后者通常会产生更清晰的代码。
或者,避开 boost,创建您自己的成员访问器填充函数。
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 afor( 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.
像 std::pair 一样,您可以编写访问或对象。
注意:您应该查看 std::pair和 std::select2nd
及其访问器: std::select1st
Like std::pair you could write access-or objects.
Note: You should look at std::pair<T1,T2>
And its accessors: std::select1st<T1> and std::select2nd<T2>
对我来说最清楚的方法是
boost::bind
:当然,您可以创建自己的成员访问函数,但我相信这会使您的代码可读性较差。 boost::bind 是众所周知的库,因此使用它将使您的代码具有良好的可读性,并且无需阅读辅助函数(偶尔可能包含错误)
我更喜欢的第二种方法是仅使用 for 循环(在这种特殊情况下 ) ):
也许使用如此简单的循环并不流行,但它们非常清晰。
The most clear way for me is
boost::bind
: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):
May be it is not fashionable to use such simple loops, but they are very clear.