c++:如何使用boost::range查找max_element

C++: how to find max_element using boost::range?

本文关键字:max element 查找 boost 何使用 c++ range      更新时间:2023-10-16

我试图返回一个迭代器到过滤范围内的最大元素。以下是目前为止的内容:

#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,并且由于它仍然是可访问的,因此您可以比较地址,因为indexedfiltered都不会复制

#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将其整齐地压缩),并通过所有转换携带原始序列索引和值。