[英]How to access Eigen Vector Matrix< float, 2, 1 >
I am iterating through a vector, which consists of Vector 我正在遍历一个由Vector组成的vector
Matrix<float, 2, 1>
for(auto it = uvVertices.begin(); it != uvVertices.end(); ++it) {
std::cout << *it;
}
this gives an output like: which works 这给出了如下输出:
0.123120.212354 0.123120.212354
which is correct, how can i access only the first or the second component? 哪个是正确的,我如何仅访问第一个或第二个组件? So that i get
这样我就可以
0.12312 0.12312
http://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html here is a reference but i could not figur eit out. http://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html这是参考,但我无法弄清楚。
If you would like to get the n
-th element of a container, you can use std::next
, like this: 如果要获取容器的第
n
个元素,可以使用std::next
,如下所示:
auto pos = 1; // Get the second element
auto it(std::next(uvVertices.begin(), k));
std::cout << *it;
The initial element can be accessed simply by dereferencing uvVertices.begin()
, like this: 只需通过取消引用
uvVertices.begin()
即可访问初始元素,如下所示:
std::cout << *(uvVertices.begin()); // Get the initial element
If I understand correctly... You can just dereference the iterator to a temporary reference inside the loop for convenience and access coefficients inside like with any Eigen
object: 如果我理解正确...您可以在循环内将迭代器取消引用为临时引用,以方便使用,并像访问任何
Eigen
对象一样访问内部的系数:
for(auto it = uvVertices.begin(); it != uvVertices.end(); ++it) {
Matrix<float, 2, 1>& v = *it;
//auto& v = *it; // should also work
std::cout << v(0,0);
std::cout << v(1,0);
}
You could also use range-for: 您还可以将范围用于:
for(auto& v : uvVertices) {
std::cout << v(0,0);
std::cout << v(1,0);
}
I would also consider using Eigen::Vector
type for a vector. 我也考虑将
Eigen::Vector
类型用于向量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.