Gael Guennebaud | 4759d9e | 2019-01-17 10:35:14 +0100 | [diff] [blame] | 1 | namespace Eigen { |
| 2 | |
| 3 | /** \eigenManualPage TutorialSTL STL iterators and algorithms |
| 4 | |
| 5 | Since the version 3.4, %Eigen's dense matrices and arrays provide STL compatible iterators. |
| 6 | As demonstrated below, this makes them naturally compatible with range-for-loops and STL's algorithms. |
| 7 | |
| 8 | \eigenAutoToc |
| 9 | |
| 10 | \section TutorialSTLVectors Iterating over 1D arrays and vectors |
| 11 | |
| 12 | Any dense 1D expressions exposes the pair of `begin()/end()` methods to iterate over them. |
| 13 | |
| 14 | This directly enables c++11 range for loops: |
| 15 | <table class="example"> |
| 16 | <tr><th>Example:</th><th>Output:</th></tr> |
| 17 | <tr><td> |
| 18 | \include Tutorial_range_for_loop_1d_cxx11.cpp |
| 19 | </td> |
| 20 | <td> |
| 21 | \verbinclude Tutorial_range_for_loop_1d_cxx11.out |
| 22 | </td></tr></table> |
| 23 | |
| 24 | One dimensional expressions can also easily be passed to STL algorithms: |
| 25 | <table class="example"> |
| 26 | <tr><th>Example:</th><th>Output:</th></tr> |
| 27 | <tr><td> |
| 28 | \include Tutorial_std_sort.cpp |
| 29 | </td> |
| 30 | <td> |
| 31 | \verbinclude Tutorial_std_sort.out |
| 32 | </td></tr></table> |
| 33 | |
| 34 | Similar to `std::vector`, 1D expressions also exposes the pair of `cbegin()/cend()` methods to conveniently get const iterators on non-const object. |
| 35 | |
| 36 | \section TutorialSTLMatrices Iterating over coefficients of 2D arrays and matrices |
| 37 | |
| 38 | STL iterators are intrinsically designed to iterate over 1D structures. |
| 39 | This is why `begin()/end()` methods are disabled for 2D expressions. |
| 40 | Iterating over all coefficients of a 2D expressions is still easily accomplished by creating a 1D linear view through `reshaped()`: |
| 41 | <table class="example"> |
| 42 | <tr><th>Example:</th><th>Output:</th></tr> |
| 43 | <tr><td> |
| 44 | \include Tutorial_range_for_loop_2d_cxx11.cpp |
| 45 | </td> |
| 46 | <td> |
| 47 | \verbinclude Tutorial_range_for_loop_2d_cxx11.out |
| 48 | </td></tr></table> |
| 49 | |
| 50 | \section TutorialSTLRowsColumns Iterating over rows or columns of 2D arrays and matrices |
| 51 | |
| 52 | It is also possible to get iterators over rows or columns of 2D expressions. |
| 53 | Those are available through the `rowwise()` and `colwise()` proxies. |
| 54 | Here is an example sorting each row of a matrix: |
| 55 | <table class="example"> |
| 56 | <tr><th>Example:</th><th>Output:</th></tr> |
| 57 | <tr><td> |
Christoph Hertzberg | e16913a | 2019-01-23 10:35:06 +0100 | [diff] [blame] | 58 | \include Tutorial_std_sort_rows_cxx11.cpp |
Gael Guennebaud | 4759d9e | 2019-01-17 10:35:14 +0100 | [diff] [blame] | 59 | </td> |
| 60 | <td> |
Christoph Hertzberg | e16913a | 2019-01-23 10:35:06 +0100 | [diff] [blame] | 61 | \verbinclude Tutorial_std_sort_rows_cxx11.out |
Gael Guennebaud | 4759d9e | 2019-01-17 10:35:14 +0100 | [diff] [blame] | 62 | </td></tr></table> |
| 63 | |
| 64 | */ |
| 65 | |
| 66 | } |