C++ 增压蓄能器是否支持计算相邻值之间的最小差值?

C++ 增压蓄能器是否支持计算相邻值之间的最小差值?,c++,boost,boost-accumulators,C++,Boost,Boost Accumulators,这里的假设是,我正在接收许多值,我不想将它们存储在向量中。 所以我想使用一些类似于增压蓄能器的东西。但在我的生活中,我找不到我想要的逻辑 有没有办法让我有一个累加器,这样当与 5、10、12、15、27它将输出两个相邻值10和12之间的最小差值 我知道我可以自己保留最后一个变量,只需使用acccurrent last with tag::min,但如果可能的话,我更愿意将其留给库。不使用Boost中的累加器,但您可以自己编写 蓄能器本身看起来像这样。请注意,此版本仅正确处理值的递增序列,但不需要

这里的假设是,我正在接收许多值,我不想将它们存储在向量中。 所以我想使用一些类似于增压蓄能器的东西。但在我的生活中,我找不到我想要的逻辑

有没有办法让我有一个累加器,这样当与

5、10、12、15、27它将输出两个相邻值10和12之间的最小差值


我知道我可以自己保留最后一个变量,只需使用acccurrent last with tag::min,但如果可能的话,我更愿意将其留给库。

不使用Boost中的累加器,但您可以自己编写

蓄能器本身看起来像这样。请注意,此版本仅正确处理值的递增序列,但不需要初始样本。如果您有不同的需求,可以对此进行调整

namespace boost {                           
namespace accumulators {                    
namespace impl {                            

template<typename Sample>
struct min_adjacent_difference_accumulator  
  : accumulator_base                        
{
    using result_type = Sample;
   
    template<typename Args>                 
    min_adjacent_difference_accumulator(Args const & args)
      : last_seen(args[sample | std::optional<Sample>{}])        
    {                                       
    }                                       
   
    template<typename Args>                 
    void operator ()(Args const & args)     
    {
        Sample new_value = args[sample];
        if (last_seen)
            min_diff = std::min(min_diff, new_value - *last_seen);
                                  
        last_seen = args[sample];
    }

    result_type result(dont_care) const     
    {                                       
        return min_diff;                   
    }
 private:
    std::optional<Sample> last_seen;
    Sample min_diff = std::numeric_limits<Sample>::max();
 };

 }}}
还有一个提取器

namespace boost {
namespace accumulators {               
namespace extract {                    

inline extractor<tag::min_adjacent_difference> const min_adjacent_difference = {}; 
                                    
}
using extract::min_adjacent_difference;                    
                                    
}}

然后,您可以像boost提供的任何蓄能器一样使用它。您可以在上查看交互式版本。

不使用作为Boost一部分的累加器,但您可以编写自己的

蓄能器本身看起来像这样。请注意,此版本仅正确处理值的递增序列,但不需要初始样本。如果您有不同的需求,可以对此进行调整

namespace boost {                           
namespace accumulators {                    
namespace impl {                            

template<typename Sample>
struct min_adjacent_difference_accumulator  
  : accumulator_base                        
{
    using result_type = Sample;
   
    template<typename Args>                 
    min_adjacent_difference_accumulator(Args const & args)
      : last_seen(args[sample | std::optional<Sample>{}])        
    {                                       
    }                                       
   
    template<typename Args>                 
    void operator ()(Args const & args)     
    {
        Sample new_value = args[sample];
        if (last_seen)
            min_diff = std::min(min_diff, new_value - *last_seen);
                                  
        last_seen = args[sample];
    }

    result_type result(dont_care) const     
    {                                       
        return min_diff;                   
    }
 private:
    std::optional<Sample> last_seen;
    Sample min_diff = std::numeric_limits<Sample>::max();
 };

 }}}
还有一个提取器

namespace boost {
namespace accumulators {               
namespace extract {                    

inline extractor<tag::min_adjacent_difference> const min_adjacent_difference = {}; 
                                    
}
using extract::min_adjacent_difference;                    
                                    
}}

然后,您可以像boost提供的任何蓄能器一样使用它。您可以在上查看交互式版本。

我在文档中也没有看到类似的内容。我认为预处理事件以获得不同的烟雾流超出了库的范围。就是按照你描述的方式使用min。自动更正。Smoke=sampleI在文档中也没有看到类似的内容。我认为预处理事件以获得不同的烟雾流超出了库的范围。就是按照你描述的方式使用min。自动更正。Smoke=samplenice,不确定样本是什么或它的作用是什么,但这是有效的。不错,不确定样本是什么或它的作用是什么,但这是有效的。