简体   繁体   中英

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> 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 . 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, ie

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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