简体   繁体   English

使用Eigen动态类型进行统一初始化

[英]Uniform initialization with Eigen dynamic types

I'm trying to learn the Eigen C++ library, and was wondering if there is some nice shorthand for initializing dynamic vectors and matrices. 我正在尝试学习Eigen C ++库,并想知道是否存在一些用于初始化动态向量和矩阵的简便方法。 It would be really nice to write something like you would using a std::vector 像使用std::vector编写类似的东西会非常好

std::vector<int> myVec = {1,2,3,6,5,4,6};

ie

VectorXi x = {1,2,3,4,7,5,7};

The closest (ugly) equivalent I can find involves Map . 我能找到的最接近(丑陋)的等效项涉及Map .

int xc[] = {2,3,1,4,5};
Map<VectorXi> x(xc,sizeof(xc)/sizeof(xc[0]));

What other initialization methods are there? 还有哪些其他初始化方法?

For fixed size matrices/vectors, you can use the comma initializer: 对于固定大小的矩阵/向量,可以使用逗号初始值设定项:

Matrix3f m;
m<<1,2,3,4,5,6,7,8,9;

I can't test it right now, but it should work similarly for your situation: 我目前无法对其进行测试,但是对于您的情况,它应该具有类似的功能:

VectorXi x(5);
x << 2,3,1,4,5;

If it does not, you could use a temporary Vector, fill it with the five elements using the comma initializer and then assign it to the VectorXi. 如果没有,则可以使用一个临时Vector,使用逗号初始化程序将其填充五个元素,然后将其分配给VectorXi。

edit: You might also be interested in this page: Eigen: Advanced Initialization 编辑:您可能也对此页面感兴趣: 特征:高级初始化

By the code you showed, you are OK in writing const items. 通过显示的代码,您可以编写const项目。 So maybe you can do something like 所以也许你可以做类似的事情

std::vector<int> vec;
const int init_vec[5] =  {1,2,3,4,5}
vec.assign(init_vec, init_vec + 5);

See this post of how use array to populate vectors. 有关如何使用数组填充向量的信息 ,请参见这篇文章

EDIT: Correct a wrong link formating. 编辑:更正错误的链接格式。

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

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