标准库算法平均相邻元素之间的差异
Standard library algorithm to average the difference between adjacent elements
我有一个 std::vector<std:::chrono::system_clock::time_point>
值。
我想平均相邻元素之间的时间差(以毫秒为单位)。容器至少需要 2 个元素,我在使用平均函数时遇到了问题。我正在使用 std::accumulate 但我不认为我能完全让它工作,因为我遇到了大量的编译器错误。下面的 lambda 用作累加算法的 reduce 函数
auto gLambda = [&](
system_clock::time_point t1, system_clock::time_point t2) {
return duration_cast<milliseconds>(t2 - t1).count();
};
虽然工作正常
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);
我有一个 std::vector<std:::chrono::system_clock::time_point>
值。
我想平均相邻元素之间的时间差(以毫秒为单位)。容器至少需要 2 个元素,我在使用平均函数时遇到了问题。我正在使用 std::accumulate 但我不认为我能完全让它工作,因为我遇到了大量的编译器错误。下面的 lambda 用作累加算法的 reduce 函数
auto gLambda = [&](
system_clock::time_point t1, system_clock::time_point t2) {
return duration_cast<milliseconds>(t2 - t1).count();
};
虽然工作正常
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);