Re: How to pass a huge matrix to a function
Erik Wikstr??m wrote in message...
On 2007-09-19 15:06, Bernhard Reinhardt wrote:
g.vukoman@gmail.com wrote:
Ok it seems I??m on the right way. I already convert my array[][][][] to
a vector < vector < vector < vector <double> > > >
I??m partly working with the vector an partly with the normal array
because I don??t know how to address a unique element of the matrix in
vector form.
I can do
vector <double> a;
cout<<a[0];
but
vector <vector <double>> b;
cout<<b[0][0];
wont compile.
It is a defect in the language, you must put a space between the two >
or the compiler will interpret it as the >> operator, i.e.
vector<vector<double> > b;
Notice that your code above will have undefined behaviour since the
vectors are empty, so trying to access an non-existent element is a bad
idea.
OP: To watch that happen, try this (cut&paste):
#include <iostream>
#include <vector>
#include <stdexcept>
int main(){
std::vector<std::vector<double> > vvd;
try{
std::cout<< vvd.at(0).at(0); // cout<<vvd[0][0];
} // try
catch( std::out_of_range const &Oor ){
std::cout<<"caught "<<Oor.what()<<std::endl;
}
return 0;
} // main()
You can init them:
std::vector<std::vector<double> > vvd( 2, 2 ); // a 2x2
std::vector<std::vector<double> > vvd24( 2,
std::vector<double>(4, 3.14 ) ); // a 2x4 all inited to 3.14
Quad init gets ugly, unless you use typedef(s):
std::vector<std::vector<std::vector<
std::vector<double> > > > QuadArray( 2,
std::vector<std::vector<std::vector<double> > >( 3,
std::vector<std::vector<double> >( 4, 5 ) ) ); //
2x3x4x5
QuadArray.at( 1 ).at( 2 ).at( 3 ).at( 4 ) = 42.31;
// same: QuadArray[1][2][3][4] = 42.31;
--
Bob R
POVrookie