元素元组加法 [英] Element-wise tuple addition
本文介绍了元素元组加法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在元组中保存了一些值,并且我希望在元素方面向其添加另一个元组.所以我想要这样的功能:
I have some values held in a tuple, and I am looking to add another tuple to it element-wise. So I would like functionality like this:
std::tuple<int,int> a = {1,2};
std::tuple<int,int> b = {2,4};
std::tuple<int,int> c = a + b; // possible syntax 1
a += b; // possible syntax 2
a += {2,4}; // possible syntax 3
其中输出元组的值为 {3,6}
Was looking at CPP reference, but I couldn't find this functionality. It's possible that this and this question are relevant, however the answers are obfuscated by other complexities.
推荐答案
您还可以考虑使用 std :: valarray ,因为它完全允许您似乎想要的东西.
You could also consider using std::valarray since it allows exactly the things that you seem to want.
#include <valarray>
int main()
{
std::valarray<int> a{ 1, 2 }, b{ 2, 4 }, c;
c = a - b; // c is {-1,-2}
a += b; // a is {3,6}
a -= b; // a is {1,2} again
a += {2, 4}; // a is {3,6} again
return 0;
}
这篇关于元素元组加法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文