简体   繁体   English

从std :: vector映射 <unsigned> 到Eigen :: VectorXi

[英]Map from std::vector<unsigned> to Eigen::VectorXi

Is there a way to directly map a std::vector<unsigned> to an Eigen::VectorXi in the following fashion? 有没有一种方法可以按照以下方式将std::vector<unsigned>直接映射到Eigen::VectorXi

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

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 . 不会“直接将std::vector<unsigned>映射到Eigen::VectorXi ”,而是将数据复制到新的Eigen::VectorXi If you want to wrap the existing data array with Eigen functionality, you should use something like: 如果要使用Eigen功能包装现有数据数组,则应使用以下方法:

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 ). 然后,您可以使用使用wrappedData就像任何其他VectorXi与调整基础数据的异常(即仍然由国有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, ie 如果您试图避免将数据复制到std::vector<int>则可以使用“自定义”矩阵类型,即

typedef Eigen::Matrix<unsigned int, Eigen::Dynamic, 1> VectorXui;
Eigen::Map<VectorXui> wrappedData(a.data(), a.size());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM