Reputation: 57
Is there a way to directly map a std::vector<unsigned>
to an Eigen::VectorXi
in the following fashion?
std::vector<unsigned> a = {1,2,3,4,5};
Eigen::VectorXi c = Eigen::VectorXi::Map(a.data(), a.size());
I'd like to skip the following step in between:
std::vector<int> b(a.begin(),a.end());
Upvotes: 0
Views: 908
Reputation: 10596
Your line
Eigen::VectorXi c = Eigen::VectorXi::Map(a.data(), a.size());
doesn't "directly map a std::vector<unsigned>
to an Eigen::VectorXi
," rather, it copies the data to a new Eigen::VectorXi
. If you want to wrap the existing data array with Eigen functionality, you should use something like:
Eigen::Map<Eigen::VectorXi> wrappedData(a.data(), a.size());
You can then use use wrappedData
as you would any other VectorXi
with the exception of resizing the underlying data (that's still owned by the std::vector
). See the documentation for more details.
If you're trying to avoid copying the data to a std::vector<int>
then you can use a "custom" matrix type, i.e.
typedef Eigen::Matrix<unsigned int, Eigen::Dynamic, 1> VectorXui;
Eigen::Map<VectorXui> wrappedData(a.data(), a.size());
Upvotes: 4