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

Go to the source code of this file.

Functions

void initialize (std::vector< double > &x)
 The class std::vector contains size information and can be instantiated for any data type. More...
 
double sum_elements (std::vector< double > &x)
 
int main ()
 

Function Documentation

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

The class std::vector contains size information and can be instantiated for any data type.

Here we do it for double. We pass a reference to the vector to the functions using it.

Examples:
03-cxx-style-ref.cxx.

Definition at line 15 of file 03-cxx-style-ref.cxx.

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

Definition at line 21 of file 03-cxx-style-ref.cxx.

22 {
23  double sum=0;
24  for (int i=0;i<x.size();i++)sum+=x[i];
25  return sum;
26 }
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:
03-cxx-style-ref.cxx.

Definition at line 28 of file 03-cxx-style-ref.cxx.

29 {
30  const int n=12345678;
31 
32  // Instantiate a vector of doubles of given size.
33  // The class object itself is placed on the stack,
34  // but the data is placed on the heap.
35  std::vector<double> x(n);
36 
37  initialize(x);
38  double s=sum_elements(x);
39  printf("sum=%e\n",s);
40 
41  // When leaving this scope, x is destroyed, and with
42  // this also the memory on the heap is freed.
43  return 0;
44 }
void initialize(std::vector< double > &x)
The class std::vector contains size information and can be instantiated for any data type...
double sum_elements(std::vector< double > &x)

+ Here is the call graph for this function: