NUMCXX  0.13.20181108
Numerical library for small projects and teaching purposes
Functions
05-cxx-style-sharedptr2.cxx File Reference
+ Include dependency graph for 05-cxx-style-sharedptr2.cxx:

Go to the source code of this file.

Functions

void initialize (std::vector< double > &x)
 We pass (as in example 03) a reference to the vector to the functions using it. More...
 
double sum_elements (std::vector< double > &x)
 
int main ()
 

Function Documentation

void initialize ( std::vector< double > &  x)

We pass (as in example 03) a reference to the vector to the functions using it.

This is possible as long as the function itself does not store the vector in another class.

Examples:
05-cxx-style-sharedptr2.cxx.

Definition at line 17 of file 05-cxx-style-sharedptr2.cxx.

18 {
19  const int n=x.size();
20  for (int i=0;i<n;i++) x[i]= 1.0/(double)(1+n-i);
21 }
double sum_elements ( std::vector< double > &  x)
Examples:
05-cxx-style-sharedptr2.cxx.

Definition at line 23 of file 05-cxx-style-sharedptr2.cxx.

24 {
25  double sum=0;
26  for (int i=0;i<x.size();i++)sum+=x[i];
27  return sum;
28 }
A::value_type sum(const A &a)
Sum of array or expression.
Definition: util.ixx:81

+ Here is the call graph for this function:

int main ( void  )
Examples:
05-cxx-style-sharedptr2.cxx.

Definition at line 30 of file 05-cxx-style-sharedptr2.cxx.

31 {
32  const int n=12345678;
33 
34  // This is a shortcut to the cumbersome statement from
35  // Example 04. We have to write the data type only once,
36  // as a template argument to std::make_shared<>.
37  auto x=std::make_shared<std::vector<double>>(n);
38 
39  // Dereference the smart pointer here in order to
40  // obtain a reference which can be used as an argumet to the functions.
41  initialize(*x);
42  double s=sum_elements(*x);
43  printf("sum=%e\n",s);
44 
45  // The shared pointer is destroyed, reference counter decremented
46  // and, as it becomes 0, the allocated memory is released.
47  return 0;
48 }
void initialize(std::vector< double > &x)
We pass (as in example 03) a reference to the vector to the functions using it.
double sum_elements(std::vector< double > &x)

+ Here is the call graph for this function: