由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教一个boost::bind的问题
相关主题
呼唤大侠们,我实在不能实现C++泛型的精神。请教C++中的unordered_set
基本功不扎实,问个问题基于macro的meta programming真难懂
用STL map的时候怎么自己定义大小比较的关系stl Compare为何需要重载()?
template 疑问请教如何自己C++编程牛逼些
请教一个C++ typedef的问题lambda 什么时候进入 c++的?
问两个C++语法问题C++要是有null object就好了
请教C++ call-by-ref & call-by-val的问题C++11痛并快乐着
C++ template Questions (转载)把一个function pointer作为参数传入一个function的语法是什么?
相关话题的讨论汇总
话题: itor话题: x2话题: x1话题: double话题: compare
进入Programming版参与讨论
1 (共1页)
d*******h
发帖数: 642
1
class A
{
double d_x1, d_x2;
vector vec;
bool Compare(double x1, double x2) const { return (d_x1==x1 && d_x2==x2);}
public:
A* Find(double x1, double x2) const
{
vector::iterator itor;
itor = find_if(vec.begin(), vec.end(), boost::bind(&A::Compare, _1, _2)(x1, x2)); // 这里编译不通过
return (*itor);
}
};
我想在Find函数里面实现的是:
for(vector::iterator itor=vec.begin();itor!=vec.end();++itor)
{
if ((*itor)->Compare(double x1, double x2))
{
X****r
发帖数: 3557
2
Haven't tried myself, but it seems that you want
boost::bind(&A::Compare, _1, x1, x2)
By the way, boost won't make your code faster here, so I don't see
why you want to write code that you don't understand yourself.

d_x2==x2);}
_1, _2)(x1, x2)); // 这里编译不通过

【在 d*******h 的大作中提到】
: class A
: {
: double d_x1, d_x2;
: vector vec;
: bool Compare(double x1, double x2) const { return (d_x1==x1 && d_x2==x2);}
: public:
: A* Find(double x1, double x2) const
: {
: vector::iterator itor;
: itor = find_if(vec.begin(), vec.end(), boost::bind(&A::Compare, _1, _2)(x1, x2)); // 这里编译不通过

d*******h
发帖数: 642
3
thanks, but still got compile error. 还有,不是说使用for_each, find_if这些函
数会比自己用for loop 快一点吗?

【在 X****r 的大作中提到】
: Haven't tried myself, but it seems that you want
: boost::bind(&A::Compare, _1, x1, x2)
: By the way, boost won't make your code faster here, so I don't see
: why you want to write code that you don't understand yourself.
:
: d_x2==x2);}
: _1, _2)(x1, x2)); // 这里编译不通过

p***o
发帖数: 1252
4
这个地方自己写个functor也不费事,要是你的code里就这一个地方用到了boost,
接手你code的人肯定要骂死你。

【在 d*******h 的大作中提到】
: thanks, but still got compile error. 还有,不是说使用for_each, find_if这些函
: 数会比自己用for loop 快一点吗?

X****r
发帖数: 3557
5
我试了一下,是const correctness的问题。你把
std::vector::iterator itor;
改为
std::vector::const_iterator itor;
就可以了。

【在 d*******h 的大作中提到】
: thanks, but still got compile error. 还有,不是说使用for_each, find_if这些函
: 数会比自己用for loop 快一点吗?

X****r
发帖数: 3557
6
里的确有可能会比自己写for loop快,但是对于vector来说我觉得不会有
多少
区别。如果你不是想要学习精通C++,而只是想要用它完成你的工作的话,为了莫须有的
局部性能
而降低代码的可读性和可维护性实在是得不偿失的事情。

【在 d*******h 的大作中提到】
: thanks, but still got compile error. 还有,不是说使用for_each, find_if这些函
: 数会比自己用for loop 快一点吗?

d*******h
发帖数: 642
7
多谢回答!
w***g
发帖数: 5958
8
折腾bind不值得。虽然代码写起来简单了,但是编译出来的代码大的吓人,一个最简单
的语法都几兆几兆的。

x2);}
_2)(x1, x2)); // 这里编译不通过

【在 d*******h 的大作中提到】
: class A
: {
: double d_x1, d_x2;
: vector vec;
: bool Compare(double x1, double x2) const { return (d_x1==x1 && d_x2==x2);}
: public:
: A* Find(double x1, double x2) const
: {
: vector::iterator itor;
: itor = find_if(vec.begin(), vec.end(), boost::bind(&A::Compare, _1, _2)(x1, x2)); // 这里编译不通过

1 (共1页)
进入Programming版参与讨论
相关主题
把一个function pointer作为参数传入一个function的语法是什么?请教一个C++ typedef的问题
c的函数指针能不能弄得像matlab的function handle一样?问两个C++语法问题
大牛给讲讲monad吧?请教C++ call-by-ref & call-by-val的问题
Learn monad in 10 minutesC++ template Questions (转载)
呼唤大侠们,我实在不能实现C++泛型的精神。请教C++中的unordered_set
基本功不扎实,问个问题基于macro的meta programming真难懂
用STL map的时候怎么自己定义大小比较的关系stl Compare为何需要重载()?
template 疑问请教如何自己C++编程牛逼些
相关话题的讨论汇总
话题: itor话题: x2话题: x1话题: double话题: compare