gpt4 book ai didi

c++ - boost 变换迭代器和 c++11 lambda

转载 作者:IT老高 更新时间:2023-10-28 23:17:30 26 4
gpt4 key购买 nike

我正在尝试通过向适配器提供 c++0x lambda 来使用 boost::adaptors::transformed。

以下代码无法编译。我正在使用带有 boost 1.48 的 g++ 4.6.2。

#include <iostream>
#include <vector>

#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>

using namespace std;
namespace br = boost::range;
namespace badpt = boost::adaptors;


int main()
{
vector<int> a = {0,3,1,};
vector<int> b = {100,200,300,400};

auto my_ftor = [&b](int r)->int{return b[r];};

cout<<*br::max_element(a|badpt::transformed(my_ftor))<<endl;
}

关于我在这里做错了什么有什么想法吗?

最佳答案

这是众所周知的问题。看这里

http://boost.2283326.n4.nabble.com/range-cannot-use-lambda-predicate-in-adaptor-with-certain-algorithms-td3560157.html

简而言之,你应该使用这个宏

#define BOOST_RESULT_OF_USE_DECLTYPE

使用 decltype 代替 boost::result_of

引自 here

If your compiler supports decltype, then you can enable automatic result type deduction by defining the macro BOOST_RESULT_OF_USE_DECLTYPE, as in the following example.

关于c++ - boost 变换迭代器和 c++11 lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12672372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com