c++vectoreigen

When to use Eigen::Vector vs std::vector?


Because of external dependencies, I'm required to write some data in a Eigen::VectorXi container.

However, there are other times in which I'm not required to use en Eigen::VectorXi as a container, and I may use a standard std::vector<int>

Currently I have Eigen and std vectors sprinkled through my code. The only times in which I decide to use an std:vector is when I'll be making insertions on the fly, since resizing and reinserting is more tedious (and possibly computationally expensive) than using push_back(<value>)

I'd like to be sure that I'm using one or the other in the right time, instead of randomly choosing. What are the right instances to use Eigen::Vector instead of std::vector?


Solution

  • Classes in Eigen represent mathematical objects. In this case, a mathematical vector, with all the typical operations on vectors, like multiplication with a matrix or scalar product.

    std::vector is essentially an (dynamic) array, storing a list of data in consecutive space. Typical operations would be appending data or sorting it.

    When you decide what you want to use, simply ask yourself if this is a mathematical vector. You could extend both to hold what the other is supposed to, creating a scalar product over std::vector, but why would you? Eigen does that already. Using it for what it is intended makes it clear to everybody what you represent, increasing readability.