我需要总结一些向量;也就是说,我想总结nth
每个向量的元素,并用结果创建一个新向量。 (我已经确保输入向量的大小都相同。)我想用优秀的range-v3 https://github.com/ericniebler/range-v3/图书馆。我试过了this https://wandbox.org/permlink/ZENcLXRLHkVtJ1BT:
// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <map>
#include <range/v3/all.hpp>
int main()
{
std::cout << "Hello, Wandbox!" << std::endl;
std::vector< int > v1{ 1,1,1};
std::vector< int> v2{1,1,1};
auto va = ranges::view::zip( v1, v2 )
| ranges::view::transform(
[](auto&& tuple){ return ranges::accumulate( tuple, 0.0 ); }
);
}
我收到无法调用的错误ranges::accumulate
像这样。我觉得这是一件简单的事情,我只是不太明白。
请指教
编辑:
我在这里问一个后续问题:如何使用 range-v3 压缩向量的向量 https://stackoverflow.com/questions/61819277/how-to-zip-vector-of-vector-with-range-v3
您可以使用std::apply https://en.cppreference.com/w/cpp/utility/apply对元组的值求和,而不是accumulate
:
auto sum_tuple = [](auto&& tuple) {
return std::apply([](auto... v) {
return (v + ...);
}, tuple );
};
auto va = ranges::views::zip( v1, v2 )
| ranges::views::transform(sum_tuple);
这是一个demo https://godbolt.org/z/BPMZW7。还显示了具有 2 个以上向量的示例。
另外,请注意ranges::view
已被弃用,取而代之的是ranges::views
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)