Re: Array of point to arrays of different size
On Nov 14, 4:57 pm, "Edward Jensen" <edw...@jensen.invalid> wrote:
Hi,
I have the following static arrays of different size in a class:
in header:
static double w2[2], x2[2];
static double w3[3], x3[3];
static double w4[4], x4[4];
in GaussLegendre.cpp:
double GaussLegendre::w2[2] = {1.0, 1.0};
double GaussLegendre::x2[2] = {-0.577350269189625, 0.577350269189625};
double GaussLegendre::w3[3] = {0.555555555555556, 0.8888888888888889,
0.555555555555556};
double GaussLegendre::x3[3] = {-0.774596669241483, 0.0, 0.7745966692414=
83};
double GaussLegendre::w4[4] = {0.347854845137453, 0.652145154862546,
0.652145154862546, 0.347854845137453};
double GaussLegendre::x4[4] = {-0.861136311594053, -0.339981043584856,
0.339981043584856, 0.861136311594053};
Now I want to create a static array of pointers of x2, x3, x4 and w2,w2,w=
4.
I thought I could do that with just
declaring statis double* x[3] in my header and the following in .cpp:
double* GaussLegendre::x[3] = {&GaussLegendre::x2, &GaussLegendre::x3,
&GaussLegendre::x4};
but I get the following error: cannot convert from 'double (*)[2]' to
'double *
Why is that and how can it be done?
Thanks in advance,
Edward
You can point to a double using a double*, you certainly can't point
to an array of doubles of varying sizes. You can however point to the
first element of each array. Something as follows:
double* GaussLegendre::x[3] = { &GaussLegendre::x2[0],
&GaussLegendre::x3[0],
&GaussLegendre::x4[0] };
However, the elements of x know nothing about the contents of the
arrays that are part of.
To solve the issue, use dynamic containers. Lets take a std::deque of
std::vectors< double > for example (ignore the operator<< for now - it
just displays the populated containers). Notice how much simpler
coding without pointers is.
#include <iostream>
#include <ostream>
#include <deque>
#include <vector>
#include <algorithm>
#include <iterator>
namespace D
{
std::deque< std::vector< double > > dvd;
}
template < typename T >
std::ostream& operator<<( std::ostream& os,
std::deque< std::vector< T > >& r_d )
{
typedef typename std::deque< std::vector< T > >::iterator DIter;
for( DIter iter = r_d.begin(); iter != r_d.end(); ++iter )
{
std::copy( (*iter).begin(),
(*iter).end(),
std::ostream_iterator< T >( os, " ") );
os << std::endl;
}
return os;
}
int main()
{
using namespace D;
std::vector< double > v1(10, 1.1); // 10 elements all initialized
std::vector< double > v2(11, 2.2);
std::vector< double > v3(12, 3.3);
dvd.push_back( v1 );
dvd.push_back( v2 );
dvd.push_back( v3 );
std::cout << dvd;
}
/*
1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1
2.2 2.2 2.2 2.2 2.2 2.2 2.2 2.2 2.2 2.2 2.2
3.3 3.3 3.3 3.3 3.3 3.3 3.3 3.3 3.3 3.3 3.3 3.3
*/
// Both the elements (std::vector< double >) and the deque itself
remain dynamic.
dvd[0].push_back( 9.9 ); // add an eleventh double to 1st collection
// How about loading a std::deque named d1 with
// four std::vector< double > containers
// all having 10 elements initialized to 0.1:
std::deque< std::vector< double > > d1(4, std::vector< double >(10,
0.1));
// Thats one line, and the containers remain dynamic.
// Still thinking of using primitive arrays and pointers?