Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there an elegant way to perform arithmetic on tuples (eg. pairs) in c++?

Tags:

c++

The title pretty much says it all. I would like to do:

std::pair<int,int> a(2,1), b(1,1), c(0,0);
c=a-b;

and get c=(1,0). If it involves defining a new class and doing operator overloading that's ok I suppose, I'd still be interested in seeing the most elegant way to do that, but it would be even better, imo, if there was a non-boost, non-new-class-definition solution. Thanks! -Mark

like image 678
mtopinka Avatar asked Dec 02 '22 15:12

mtopinka


1 Answers

std::valarray is designed to support numerical computation on vectors (that is, ordered sequences of numbers).

std::valarray<int> a {2, 1}, b {1, 1}, c;
c = a - b;

You can access its elements like you would access those of std::vector, i.e., with the subscripting operator [].

like image 159
Brian Bi Avatar answered Dec 20 '22 08:12

Brian Bi