aboutsummaryrefslogtreecommitdiffhomepage
path: root/doc/snippets/Tutorial_Map_using.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'doc/snippets/Tutorial_Map_using.cpp')
-rw-r--r--doc/snippets/Tutorial_Map_using.cpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/doc/snippets/Tutorial_Map_using.cpp b/doc/snippets/Tutorial_Map_using.cpp
new file mode 100644
index 000000000..e5e499f1f
--- /dev/null
+++ b/doc/snippets/Tutorial_Map_using.cpp
@@ -0,0 +1,21 @@
+typedef Matrix<float,1,Dynamic> MatrixType;
+typedef Map<MatrixType> MapType;
+typedef Map<const MatrixType> MapTypeConst; // a read-only map
+const int n_dims = 5;
+
+MatrixType m1(n_dims), m2(n_dims);
+m1.setRandom();
+m2.setRandom();
+float *p = &m2(0); // get the address storing the data for m2
+MapType m2map(p,m2.size()); // m2map shares data with m2
+MapTypeConst m2mapconst(p,m2.size()); // a read-only accessor for m2
+
+cout << "m1: " << m1 << endl;
+cout << "m2: " << m2 << endl;
+cout << "Squared euclidean distance: " << (m1-m2).squaredNorm() << endl;
+cout << "Squared euclidean distance, using map: " <<
+ (m1-m2map).squaredNorm() << endl;
+m2map(3) = 7; // this will change m2, since they share the same array
+cout << "Updated m2: " << m2 << endl;
+cout << "m2 coefficient 2, constant accessor: " << m2mapconst(2) << endl;
+/* m2mapconst(2) = 5; */ // this yields a compile-time error