NUMCXX  0.13.20181108
Numerical library for small projects and teaching purposes
12-numcxx-sharedptr.cxx
Go to the documentation of this file.
1 
7 #include <cstdio>
8 #include <vector>
9 #include <memory>
10 #include <numcxx/numcxx.hxx>
11 
12 // initialize vector x with some data
14 {
15  const int n=X.size();
16  for (int i=0;i<n;i++) X(i)= 1.0/(double)(1+n-i);
17 }
18 
19 // calculate the sum of the elements of x
21 {
22  double sum=0;
23  for (int i=0;i<X.size();i++)sum+=X(i);
24  return sum;
25 }
26 
27 int main()
28 {
29  const int n=1.0e7;
30  // call constructor and wrap pointer into smart pointer
32  initialize(*pX); // dereference pointer to obtain reference
33  double s=sum_elements(*pX); // dereference pointer to obtain reference
34  printf("sum=%e\n",s);
35  // smartpointer calls destructor if reference count
36  // reaches zero
37 }
int main()
void initialize(numcxx::DArray1 &X)
size_t size() const
Obtain size of array.
Definition: tarray.hxx:58
Main header of the library.
One dimensional array class.
Definition: tarray1.hxx:31
A::value_type sum(const A &a)
Sum of array or expression.
Definition: util.ixx:81
static std::shared_ptr< TArray1< T > > create(index n1)
Construct smart pointer empty 1D Array.
Definition: tarray1.hxx:81
double sum_elements(numcxx::DArray1 &X)