source: pacpussensors/trunk/Vislab/lib3dv/eigen/Eigen-install/doc/snippets/compile_Tutorial_Map_using.cpp@ 136

Last change on this file since 136 was 136, checked in by ldecherf, 7 years ago

Doc

File size: 1.1 KB
Line 
1#include <Eigen/Dense>
2#include <iostream>
3
4using namespace Eigen;
5using namespace std;
6
7int main(int, char**)
8{
9 cout.precision(3);
10 typedef Matrix<float,1,Dynamic> MatrixType;
11typedef Map<MatrixType> MapType;
12typedef Map<const MatrixType> MapTypeConst; // a read-only map
13const int n_dims = 5;
14
15MatrixType m1(n_dims), m2(n_dims);
16m1.setRandom();
17m2.setRandom();
18float *p = &m2(0); // get the address storing the data for m2
19MapType m2map(p,m2.size()); // m2map shares data with m2
20MapTypeConst m2mapconst(p,m2.size()); // a read-only accessor for m2
21
22cout << "m1: " << m1 << endl;
23cout << "m2: " << m2 << endl;
24cout << "Squared euclidean distance: " << (m1-m2).squaredNorm() << endl;
25cout << "Squared euclidean distance, using map: " <<
26 (m1-m2map).squaredNorm() << endl;
27m2map(3) = 7; // this will change m2, since they share the same array
28cout << "Updated m2: " << m2 << endl;
29cout << "m2 coefficient 2, constant accessor: " << m2mapconst(2) << endl;
30/* m2mapconst(2) = 5; */ // this yields a compile-time error
31
32 return 0;
33}
Note: See TracBrowser for help on using the repository browser.