有什么简单的方法可以实现 std::array 和 std::vector 的算术运算?

use*_*020 1 c++ arrays containers vector

有什么简单的方法可以为and重载各种算术运算符(+, -, *, /, +=, ...)?算术运算是按成员进行的。类似于以下内容std::arraystd::vector

template<class ContainerT, class opT>
ContainerT operator opT(ContainerT const& a, ContainerT const& b)
{
    ....
}

template<class ContainerT, class opT>
ContainerT operator opT=(ContainerT const& a, ContainerT const& b)
{
    ....
}
Run Code Online (Sandbox Code Playgroud)

Nei*_*irk 5

std::vector<int> input1(..);
std::vector<int> input2(..);
std::vector<int> output(input1.size());
std::transform (input1.begin(), input1.end(), input2.begin(), output.begin(), std::plus<int>());
Run Code Online (Sandbox Code Playgroud)

还有其他函子可供选择,您可以随时创建自己的函子或使用 lambda。