NUMCXX
0.13.20181108
Numerical library for small projects and teaching purposes
Main Page
Related Pages
Namespaces
Classes
Files
Examples
03-cxx-style-ref.cxx
Working with C++ STL vectors passed as references.This uses the class
std::vector
.
#include <cstdio>
#include <vector>
void
initialize
(std::vector<double>& x)
{
const
int
n=x.size();
for
(
int
i=0;i<n;i++) x[i]= 1.0/(
double
)(1+n-i);
}
double
sum_elements
(std::vector<double>& x)
{
double
sum
=0;
for
(
int
i=0;i<x.size();i++)sum+=x[i];
return
sum
;
}
int
main
()
{
const
int
n=12345678;
// Instantiate a vector of doubles of given size.
// The class object itself is placed on the stack,
// but the data is placed on the heap.
std::vector<double> x(n);
initialize
(x);
double
s=
sum_elements
(x);
printf(
"sum=%e\n"
,s);
// When leaving this scope, x is destroyed, and with
// this also the memory on the heap is freed.
return
0;
}
Generated on Thu Nov 8 2018 22:52:26 for NUMCXX by
1.8.11