Element-wise tuple addition

前端 未结 4 777
后悔当初
后悔当初 2021-02-06 23:29

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         


        
4条回答
  •  死守一世寂寞
    2021-02-06 23:35

    You could also consider using std::valarray since it allows exactly the things that you seem to want.

    #include 
    
    int main()
    {
        std::valarray 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;
    }
    

提交回复
热议问题