c++ - 标准库算法对相邻元素之间的差异进行平均

标签 c++ algorithm c++17 c++-chrono

我有一个std::vector<std:::chrono::system_clock::time_point>值。

我想平均相邻元素之间的时间差(以毫秒为单位)。该容器至少需要 2 个元素,但我在使用平均功能时遇到了问题。我正在使用 std::accumulate 但我认为我不能完全让它工作,因为我遇到了大量编译器错误。我将下面的 lambda 用作带有累积算法的归约函数

    auto gLambda = [&](
        system_clock::time_point t1, system_clock::time_point t2) {
            return duration_cast<milliseconds>(t2 - t1).count();
    };

工作正常

live coliru demo

int main()
{
    // lambda called with adjacent samples from the deque
    auto gLambda = [&](
        system_clock::time_point t1, system_clock::time_point t2) {
            return duration_cast<milliseconds>(t2 - t1).count();
    };
    
    // this line compiles and the lambda seems to do the right thing
    const auto t1 = system_clock::now();
    std::this_thread::sleep_for(55ms);
    const auto t2 = system_clock::now();
    // result is 55 as expected
    auto result = gLambda(t1, t2);    
    std::cout << result << std::endl;    
    // create a vector of 10 timestamps
    std::vector<system_clock::time_point> timeStamps;    
    for (auto i=0; i<10; i++) {
        timeStamps.emplace_back(system_clock::now());
        std::this_thread::sleep_for(15ms);
    }
    // THIS LINE CAUSES THE COMPILATION ERROR
    auto result1 = std::accumulate(timeStamps.cbegin(), timeStamps.cend(), 0.0, gLambda);
    std::cout << result1 << std::endl;
}

导致以下我不明白的编译错误。

In file included from /usr/local/include/c++/10.2.0/numeric:62,
                 from main.cpp:4:
/usr/local/include/c++/10.2.0/bits/stl_numeric.h: In instantiation of '_Tp std::accumulate(_InputIterator, _InputIterator, _Tp, _BinaryOperation) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >*, std::vector<std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > > > >; _Tp = double; _BinaryOperation = main()::<lambda(std::chrono::_V2::system_clock::time_point, std::chrono::_V2::system_clock::time_point)>]':
main.cpp:30:88:   required from here
/usr/local/include/c++/10.2.0/bits/stl_numeric.h:169:22: error: no match for call to '(main()::<lambda(std::chrono::_V2::system_clock::time_point, std::chrono::_V2::system_clock::time_point)>) (double&, const std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >&)'
  169 |  __init = __binary_op(_GLIBCXX_MOVE_IF_20(__init), *__first);
      |           ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:12:20: note: candidate: 'main()::<lambda(std::chrono::_V2::system_clock::time_point, std::chrono::_V2::system_clock::time_point)>'
   12 |     auto gLambda = [&](
      |                    ^
main.cpp:12:20: note:   no known conversion for argument 1 from 'double' to 'std::chrono::_V2::system_clock::time_point' {aka 'std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >'}

最佳答案

cigien评论中建议,以下是如何使用 inner_product 做到这一点:

auto result = inner_product(
                  timeStamps.begin() + 1, timeStamps.end(),
                  timeStamps.begin(), 0ms,
                  [](auto x, auto y) {return x + y;},
                  [](auto x, auto y) {return round<milliseconds>(x - y);})
              / (timeStamps.size() - 1);

关于c++ - 标准库算法对相邻元素之间的差异进行平均,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65229522/

相关文章:

c++ - std::swap 只读引用的错误分配

c++ - Shared_pointer 显示出与我在教科书中读到的行为不同的行为

algorithm - 在 mxn 矩阵中找到最大数量的不同方阵

c++ - 如何简化 std::variant 类类型

c++ - 从 vector 中提取 uint8_t* 子集<uint8_t>

c++ - thread_local 变量的实例是否保证由访问它们的线程初始化?

c++ - 如何从父类访问变量

c++ - 为什么我的 SDL/C++ 图像消失了?

python - 重新排列列表中元素的更好方法

algorithm - 密码存储于2017年