Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL multi-dimensional vector computations on rows and columns using iterators

Reply
Thread Tools

STL multi-dimensional vector computations on rows and columns using iterators

 
 
Kamil
Guest
Posts: n/a
 
      10-05-2005
I want to sum elements of a multi-dimensional vector, across each
vector, or across each dimension... this is my old code:

#define v_ldouble vector<ldouble>
#define vv_ldouble vector<vector<ldouble> >
....
void vect_sum_old(const vv_ldouble& x, v_ldouble& sum, const int dir)
{
int v,nV,d,nD;

nV=x.size();
nD=x[0].size();

if(dir!=1)
{
sum = v_ldouble(nV,0.0);
for(v=0; v<nV; v++)
for(d=0; d<nD; d++)
sum[v]+=x[v][d];
}
else
{
sum = v_ldouble(nD,0.0);
for(d=0; d<nD; d++)
for(v=0; v<nV; v++)
sum[d]+=x[v][d];
}
}

Yeah I know... excuse my brute-force engineering approach. I am new to
STL -- I switched from C to C++ because STL vectors make my life
easier. Now I want to utilize more of what STL/C++ has to offer. I can
compute the sum of each row vector using iterators:

vv_ldouble::iterator iter;
v_ldouble::iterator iter1;
vv_ldouble mdv = vv_ldouble(3,v_ldouble(4,0.0));
v_ldouble sumrow = v_ldouble(mdv.size());
v_ldouble sumcol = v_ldouble(mdv[0].size());
....
//Sum each vector across its elements...
for(iter = mdv.begin(), iter1=sumrow.begin(); iter!=mdv.end(); iter++,
iter1++)
*iter1 = accumulate((*iter).begin(), (*iter).end(), 0.0);

How do I go about computing sum of each column using iterators
(efficiently)?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
STL multi-dimensional vector computations on rows and columns using iterators Kamil C++ 1 10-05-2005 05:40 PM
STL multi-dimensional vector computations on rows and columns using iterators Kamil C++ 0 10-05-2005 01:45 PM
STL multi-dimensional vector computations on rows and columns using iterators Kamil C++ 0 10-05-2005 12:18 PM
convert rows to columns and columns to rows helpful sql ASP .Net 0 05-19-2005 06:03 PM



Advertisments