Eigen  5.0.1
 
Loading...
Searching...
No Matches
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.

Iterating over 1D arrays and vectors

Any dense 1D expression exposes the pair of begin()/end() methods to iterate over it.

This directly enables range for loops:

Example:Output:
// SPDX-FileCopyrightText: The Eigen Authors
// SPDX-License-Identifier: MPL-2.0
VectorXi v = VectorXi::Random(4);
cout << "Here is the vector v:\n";
for (auto x : v) cout << x << " ";
cout << "\n";
Here is the vector v:
1804289383 -465790871 1957747793 -1427598262 

One dimensional expressions can also easily be passed to STL algorithms:

Example:Output:
// SPDX-FileCopyrightText: The Eigen Authors
// SPDX-License-Identifier: MPL-2.0
Array4i v = Array4i::Random().abs();
cout << "Here is the initial vector v:\n" << v.transpose() << "\n";
std::sort(v.begin(), v.end());
cout << "Here is the sorted vector v:\n" << v.transpose() << "\n";
Here is the initial vector v:
1804289383  465790871 1957747793 1427598262
Here is the sorted vector v:
 465790871 1427598262 1804289383 1957747793

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

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():

Example:Output:
// SPDX-FileCopyrightText: The Eigen Authors
// SPDX-License-Identifier: MPL-2.0
Matrix2i A = Matrix2i::Random();
cout << "Here are the coeffs of the 2x2 matrix A:\n";
for (auto x : A.reshaped()) cout << x << " ";
cout << "\n";
Here are the coeffs of the 2x2 matrix A:
1804289383 -465790871 1957747793 -1427598262 

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:

Example:Output:
// SPDX-FileCopyrightText: The Eigen Authors
// SPDX-License-Identifier: MPL-2.0
ArrayXXi A = ArrayXXi::Random(4, 4).abs();
cout << "Here is the initial matrix A:\n" << A << "\n";
for (auto row : A.rowwise()) std::sort(row.begin(), row.end());
cout << "Here is the sorted matrix A:\n" << A << "\n";
Here is the initial matrix A:
1804289383 1550966999  782303108  336465782
 465790871 1122281286 1843394476  278722862
1957747793 1364114958   35005211 2145174067
1427598262  102585885 1852781081 1045969719
Here is the sorted matrix A:
 336465782  782303108 1550966999 1804289383
 278722862  465790871 1122281286 1843394476
  35005211 1364114958 1957747793 2145174067
 102585885 1045969719 1427598262 1852781081