TutorialSTL.dox 2.1 KB

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