我尝试编写一个函数,使用boost lambda库计算两个代码字之间的汉明距离.我有以下代码:
#include <iostream>
#include <numeric>
#include <boost/function.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/if.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>
template<typename Container>
int hammingDistance(Container & a, Container & b) {
return std::inner_product(
a.begin(),
a.end(),
b.begin(),
(_1 + _2),
boost::lambda::if_then_else_return(_1 != _2, 1, 0)
);
}
int main() {
boost::array<int, 3> a = {1, 0, 1}, b = {0, 1, 1};
std::cout << hammingDistance(a, b) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
而我得到的错误是:
HammingDistance.cpp: In function ‘int hammingDistance(Container&, Container&)’:
HammingDistance.cpp:15: error: no match for ‘operator+’ in ‘<unnamed>::_1 + <unnamed>::_2’ …Run Code Online (Sandbox Code Playgroud) 假设我有一个subscribe()调用的函数,它接受一个回调处理程序,当触发事件时将调用它.
现在,我有另一个版本,叫做subscribe2().一切都是相同的,除了触发时,它需要将其发布到事件队列.它是使用原始实现的subscribe(),带有一个名为helper的函数helper().它所做的只是将原始处理程序和任何其他参数绑定到仿函数中,然后调用postToEventQueue().
现在,我想知道是否有一种消除辅助函数的方法,因此subsribe2(),我可以以某种方式直接打包postToTaskQueue()函数和原始回调处理程序,并将其传递给subscribe().原因是我有很多不同的处理程序类型,并且在整个地方引入辅助函数是很乏味和累人的.毕竟,boost :: bind应该在给定原始函数的情况下返回一个新函数,对吧?我试图用boost :: bind直接生成辅助函数.
一种尝试就是说
subscribe(boost::bind(boost::bind(postToTaskQueue, boost::bind(_1, _2)), cb, _1));
Run Code Online (Sandbox Code Playgroud)
在subscribe2(),但它不起作用.有可能吗?
请参阅下面的详细示例代码.谢谢!
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
typedef boost::function<void(int)> SomeCallback;
typedef boost::function<void()> Task;
void handler(int i){
std::cout << "i=" << i <<std::endl;
}
void subscribe(SomeCallback cb)
{
cb(100); //just invoke the callback for simplicity
}
void postToTaskQueue(Task t)
{
t(); // just invoke the task for simplicity
} …Run Code Online (Sandbox Code Playgroud) 为什么回调只调用一次?
bool callback()
{
static bool res = false;
res = !res;
return res;
}
int main(int argc, char* argv[])
{
vector<int> x(10);
bool result=false;
for_each(x.begin(),x.end(),var(result)=var(result)||bind(callback));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个形式的功能:
void DoSomething(const boost::function<bool ()>& condition, other stuff);
Run Code Online (Sandbox Code Playgroud)
此函数执行一些工作,仅在条件为真时返回.这个条件已被表达为仿函数参数,因为我想在不同的呼叫站点提供不同的条件.
现在,直接使用它是相当简单的,但它需要声明许多小的一次性函数或函子对象,如果可能的话我想避免使用它们.我一直在关注Boost的lambda库,以寻找可能的方法来消除这些,但我认为我缺少一些基本的东西; 我无法让它做我想做的事.
有一个案子让我感到难过:我有一个std::vector叫做的集合data; 我所追求的条件是当该size()集合达到某个阈值时.从本质上讲,我希望我的condition仿函数在返回时返回true data.size() >= threshold,否则返回false.但是我一直在用lambda语法表达这个问题.
到目前为止我能够提出的最好的(至少编译,但它不起作用)是这样的:
boost::function<bool (size_t)> ge = boost::bind(std::greater_equal<size_t>(),
_1, threshold);
boost::function<size_t ()> size = boost::bind(&std::vector<std::string>::size,
data);
DoSomething(boost::lambda::bind(ge, boost::lambda::bind(size)), other stuff);
Run Code Online (Sandbox Code Playgroud)
在进入时DoSomething,大小为0 - 即使在运行过程中大小增加,调用condition()总是看起来大小为0.追踪它(通过Boost的内部结构有点棘手),而它似乎greater_equal每次condition()评估都会调用它,它似乎没有调用size().
那么我完全搞砸了什么基本的东西?是否有更简单的方式来表达这种事情(同时仍然保持代码尽可能内联)?
理想情况下,我希望尽可能接近C#等效代码流畅度:
DoSomething(delegate() { return data.size() >= threshold; }, other stuff);
DoSomething(() => (data.size() >= threshold), other stuff);
Run Code Online (Sandbox Code Playgroud) Boost lambda允许使用ret<T>模板覆盖推断的返回类型.我曾尝试在凤凰中搜索等效物,但找不到它.
在凤凰中有相同的东西吗?我知道怎么做自己的替换,但我宁愿不做.谢谢
让我说我有
struct Value { int foo(); };
size_t *begin = ...,
*end = ...;
Run Code Online (Sandbox Code Playgroud)
如果我想Value在C++ 03中对一堆索引进行排序,我必须写一些像这样乏味的东西:
struct Comparator
{
Value *data;
Comparator(Value *data) : data(data) { }
bool operator()(size_t a, size_t b)
{ return data[a].foo() < data[b].foo(); }
};
sort(begin, end, Comparator(data));
Run Code Online (Sandbox Code Playgroud)
有没有办法用Boost(可能是Boost.Lambda)更整齐地写出这个,最好是一行?
我是新手,并试图编写一些简单的程序来理解它.在下面的代码中,我试图用随机数填充数组.这是我的代码:
using namespace boost::lambda;
srand(time(NULL));
boost::array<int,100> a;
std::for_each(a.begin(), a.end(), _1=rand());
Run Code Online (Sandbox Code Playgroud)
但它看起来rand()只被评估一次,我的数组包含每个元素的相同值.任何人都可以指出这段代码有什么问题吗?
我正在尝试使用boost lambda来避免编写琐碎的仿函数.例如,我想使用lambda来访问结构的成员或调用类的方法,例如:
#include <vector>
#include <utility>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
using namespace std;
using namespace boost::lambda;
vector< pair<int,int> > vp;
vp.push_back( make_pair<int,int>(1,1) );
vp.push_back( make_pair<int,int>(3,2) );
vp.push_back( make_pair<int,int>(2,3) );
sort(vp.begin(), vp.end(), _1.first > _2.first );
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,我得到以下错误:
error C2039: 'first' : is not a member of 'boost::lambda::lambda_functor<T>'
with
[
T=boost::lambda::placeholder<1>
]
error C2039: 'first' : is not a member of 'boost::lambda::lambda_functor<T>'
with
[
T=boost::lambda::placeholder<2>
]
Run Code Online (Sandbox Code Playgroud)
由于vp包含pair<int,int>我认为_1.first应该工作.我做错了什么?
我使用boost :: lambda删除字符串中的后续空格,只留下一个空格.我试过这个程序.
#include <algorithm>
#include <iostream>
#include <string>
#include <boost/lambda/lambda.hpp>
int main()
{
std::string s = "str str st st sss";
//s.erase( std::unique(s.begin(), s.end(), (boost::lambda::_1 == ' ') && (boost::lambda::_2== ' ')), s.end()); ///< works
s.erase( std::unique(s.begin(), s.end(), (boost::lambda::_1 == boost::lambda::_2== ' ')), s.end()); ///< does not work
std::cout << s << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
注释行工作正常,但未注释的行没有.
怎么
(boost::lambda::_1 == boost::lambda::_2== ' ')
Run Code Online (Sandbox Code Playgroud)
不同于
(boost::lambda::_1 == ' ') && (boost::lambda::_2== ' '))
Run Code Online (Sandbox Code Playgroud)
在上面的程序.评论的那个也给了我一个警告,"警告C4805:'==':'bool'类型的不安全混合,并在操作中输入'const char'"
谢谢.
我正在研究boost :: lambda作为一种通用算法,可以使用任何类的任何"getter"方法.
该算法用于检测属性的重复值,我希望它适用于任何类的任何属性.
在C#中,我会做这样的事情:
class Dummy
{
public String GetId() ...
public String GetName() ...
}
IEnumerable<String> FindNonUniqueValues<ClassT>
(Func<ClassT,String> propertyGetter) { ... }
Run Code Online (Sandbox Code Playgroud)
示例使用方法:
var duplicateIds = FindNonUniqueValues<Dummy>(d => d.GetId());
var duplicateNames = FindNonUniqueValues<Dummy>(d => d.GetName());
Run Code Online (Sandbox Code Playgroud)
我可以使用接口或模板方法来获取"任何类"部分,但尚未找到如何使"for any method"部分工作.
有没有办法在C++中使用类似"d => d.GetId()"lambda(有或没有Boost)?
另外,更多的C++ ian解决方案也可以使算法通用.
我正在使用带有VS2008的C++/CLI,所以我不能使用C++ 0x lambdas.