I did vector calculation like blow.
Here I just use 2 vector addition C =A1+A2

But, now I want to add many vecters like A1+A2+...+A35

Is there any simple method beside
( I don't want to type continuously like A1,A2,A3,A4.....A35)

ex)

void sum(vector<long double>& C,const vector<long double>& A0,const vector<long double>& A1,..........const vector<long double>& A35)


{
for(int i=0; i<3; i++) C =A0+A1+...A35;
}

sum(C,A[0],A[1]..........A[35]);


please help me.
Thanks

------------------vector addtion program C=A1+A2---------------
#include<iostream>
#include <vector>


using namespace std;

void sum(vector<long double>& C,const vector<long double>& A0,const vector<long double>& A1)

{
for(int i=0; i<3; i++) C =A0+A1;
}


int main()
{

vector< vector<long double> > A(2);
for(int i=0; i<2; i++)
{
A.resize(3);
}

vector<long double> C(3);

A[0][0]=0.111915570000000; A[0][1]=0.642849880000000; A[0][2]=0.928839650000000;
A[1][0]=0.406786090000000; A[1][1]=-0.557532060000000; A[1][2]=-0.403578550000000;


sum(C,A[0],A[1]);
cout << "C:" << endl;
for (int i=0;i<3;i++) cout << C << endl;

}


-----------------------------------------------------------------
results:

C:
0.518702
0.0853178
0.525261

Recommended Answers

All 2 Replies

May bee this will help you

vector<long double> C sum(const vector<long double>& A0,const vector<long double>& A1)
{
......
}

Then you can write somethink like this

vector<long double>A1;
vector<long double>A2;
vector<long double>A3;
vector<long double>Rez = vector<long double>(sum(A1 ,sum(A2,A3)));

>> ... But, now I want to add many vecters like A1+A2+...+A35 ...
it would be much more flexible (also readable) to use a collection of 35 elements instead of 35 named variables of the same type. eg. vector< vector<long double> > vectors_1_to_N ; and then you can,

#include <vector>
#include <algorithm>
#include <functional>
#include <cassert>
//#include <type_traits> // c++0x
using namespace std ;

template< typename T >
void accumulate( const vector< vector<T> >& vectors, vector<T>& result )
{
  // c++0x
  //static_assert( is_arithmetic<T>::value, "type is not arithmetic" ) ; 
  if( !vectors.empty() )
  {
    //invariant: all vectors are of the same size
    result = vectors[0] ;
    const size_t N = result.size() ;
    for( size_t i = 1 ; i < vectors.size() ; ++i )
    {
      assert( vectors[i].size() == N ) ;
      transform( result.begin(), result.end(), vectors[i].begin(),
                 result.begin(), plus<T>() ) ;
    }
  }
  else result.clear() ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.