c++:如何使用boost::range查找max_element
C++: how to find max_element using boost::range?
我试图返回一个迭代器到过滤范围内的最大元素。以下是目前为止的内容:
#include <boost/lambda/lambda.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <vector>
#include <iostream>
using namespace boost::adaptors;
using namespace boost::lambda;
using namespace std;
int main ()
{
vector<double> x = {100, 150, 200, 110};
auto it = boost::max_element(x | indexed(0) | filtered(_1>100)); /* problem here */
cout << it.index() << endl;
return 0;
}
我期望代码打印出向量x中具有最大元素(即2)的索引,但不幸的是它不编译(Linux 64位,GCC 4.7.2),问题在上面指出的行中。我从编译器(以及其他)得到的第一个编译错误如下:
/boost/tuple/detail/tuple_basic.hpp:396:36:错误:只读成员' boost::tuples::cons::head '
你知道我做错了什么吗?或者我怎样才能达到我想要做的?提前感谢!
编辑:将问题行改为:
auto it = boost::max_element<boost::return_found>(x | sliced(1,4) | filtered(boost::function<bool(double)>(_1>100)));
似乎将迭代器返回到最大的元素。但是,是否有一种方法可以检查迭代器是否在该范围内?将它与boost::end(x)进行比较,我得到了一个错误。我唯一能想到的就是返回
auto another_range = boost::max_element<boost::return_found_end>(x | sliced(1,4) | filtered(boost::function<bool(double)>(_1>100)));
并检查boost::empty(another_range)。这是唯一的选择吗?谢谢。
您遇到的特定错误出现是因为boost lambda不可CopyAssignable。下面是实现相同消息的更简单的方法:
auto f1 = _1 > 100;
auto f2 = f1;
f2 = f1; // same error
如果你提供了一个CopyAssignable函子给filtered
, boost。Phoenix(无论如何都应该使用它,boost)。lambda正在逐渐被弃用,转而使用phoenix),一个手写的结构体,或者旧的忠实的std::bind2nd(std::greater<double>(), 100)
,这一行用clang++编译:
bind2nd demo: http://liveworkspace.org/code/2xKZIf
phoenix demo: http://liveworkspace.org/code/18425g
由于一些boost,它在gcc中失败。概念检查,这可能是一个错误,但这是一个有争议的问题,因为filtered
的结果是boost::filtered_range
,其迭代器不具有.index()
成员函数。
编辑回复评论:将进入filtered_range的迭代器与进入原始vector的迭代器进行比较是行不通的。然而,由于您使用了vector,并且由于它仍然是可访问的,因此您可以比较地址,因为indexed
和filtered
都不会复制
#include <vector>
#include <iostream>
#include <cassert>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/phoenix.hpp>
using namespace boost::adaptors;
using namespace boost::phoenix::placeholders;
int main ()
{
std::vector<double> x = {100, 150, 200, 110};
auto it = boost::max_element( x | indexed(0) | filtered(arg1 < 110) );
assert(&x[0] <= &*it && &*it < &x[0] + x.size());
std::cout << "Element " << *it << " is at index " << &*it - &x[0] << 'n';
}
演示http://liveworkspace.org/code/1zBIJ9 或者,对于更一般的解决方案,您可以将向量转换为一对向量(当boost获得压缩适配器时,可以使用counting_range将其整齐地压缩),并通过所有转换携带原始序列索引和值。
- <streamsize>C++ 中 numeric_limits::max() 的值
- 黑客级别的Mini-Max Sum
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- 关于 std::min, std::max 中的比较运算符的混淆
- 找到一种有效的方法,在 2 个巨大的缓冲区上执行 MAX,每字节字节
- std::max() 函数与定点实现的比较中的问题
- 使用 CImg 库的 std::min 和 std::max 的编译问题
- 在 3ds Max 中更新进度条后,环境和效果 UI 不刷新
- 从自定义类获取对象向量中的 max 元素
- 如何在C++中递归地找到max元素的索引?
- SFINAE 与 numeric_limits<T>::max() 在 MSVC2017 上
- 这句话是什么意思 - " vector<long long> distance(n, std::numeric_limits<long long>::max()); "?
- 如何将 bsoncxx::d ocument::element 写入控制台
- 在f2c.h文件中定义min()max()宏时出错
- c++curl返回413请求实体过大,但是post大小远小于max大小
- 在枚举类型上使用std::max是不是一种糟糕的做法
- 为什么Visual Studio 2019不支持用于减少Openmp的关键字"max"?
- 没有函数模板的实例"max"与参数列表参数类型匹配(int、int)
- 如何在 C++11 中将 std::max 与自定义比较器一起使用?
- 为什么在这种情况下不能将 Max 用作函数?