aboutsummaryrefslogtreecommitdiffhomepage
path: root/doc/TutorialSTL.dox
blob: 9a825bc4885a2d0db802db59cd885a206ce77fb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
namespace Eigen {

/** \eigenManualPage TutorialSTL STL iterators and algorithms

Since the version 3.4, %Eigen's dense matrices and arrays provide STL compatible iterators.
As demonstrated below, this makes them naturally compatible with range-for-loops and STL's algorithms.

\eigenAutoToc

\section TutorialSTLVectors Iterating over 1D arrays and vectors 

Any dense 1D expressions exposes the pair of `begin()/end()` methods to iterate over them.

This directly enables c++11 range for loops:
<table class="example">
<tr><th>Example:</th><th>Output:</th></tr>
<tr><td>
\include Tutorial_range_for_loop_1d_cxx11.cpp
</td>
<td>
\verbinclude Tutorial_range_for_loop_1d_cxx11.out
</td></tr></table>

One dimensional expressions can also easily be passed to STL algorithms:
<table class="example">
<tr><th>Example:</th><th>Output:</th></tr>
<tr><td>
\include Tutorial_std_sort.cpp
</td>
<td>
\verbinclude Tutorial_std_sort.out
</td></tr></table>

Similar to `std::vector`, 1D expressions also exposes the pair of `cbegin()/cend()` methods to conveniently get const iterators on non-const object.

\section TutorialSTLMatrices Iterating over coefficients of 2D arrays and matrices

STL iterators are intrinsically designed to iterate over 1D structures.
This is why `begin()/end()` methods are disabled for 2D expressions.
Iterating over all coefficients of a 2D expressions is still easily accomplished by creating a 1D linear view through `reshaped()`:
<table class="example">
<tr><th>Example:</th><th>Output:</th></tr>
<tr><td>
\include Tutorial_range_for_loop_2d_cxx11.cpp
</td>
<td>
\verbinclude Tutorial_range_for_loop_2d_cxx11.out
</td></tr></table>

\section TutorialSTLRowsColumns Iterating over rows or columns of 2D arrays and matrices

It is also possible to get iterators over rows or columns of 2D expressions.
Those are available through the `rowwise()` and `colwise()` proxies.
Here is an example sorting each row of a matrix:
<table class="example">
<tr><th>Example:</th><th>Output:</th></tr>
<tr><td>
\include Tutorial_std_sort_rows_cxx11.cpp
</td>
<td>
\verbinclude Tutorial_std_sort_rows_cxx11.out
</td></tr></table>

*/

}