簡體   English   中英

如何訪問特征向量矩陣<float,2,1>

[英]How to access Eigen Vector Matrix< float, 2, 1 >

我正在遍歷一個由Vector組成的vector

Matrix<float, 2, 1>

for(auto it = uvVertices.begin(); it != uvVertices.end(); ++it) {
    std::cout << *it;  
}

這給出了如下輸出:

0.123120.212354

哪個是正確的,我如何僅訪問第一個或第二個組件? 這樣我就可以

0.12312

http://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html這是參考,但我無法弄清楚。

如果要獲取容器的第n個元素,可以使用std::next ,如下所示:

auto pos = 1; // Get the second element
auto it(std::next(uvVertices.begin(), k));
std::cout << *it;

只需通過取消引用uvVertices.begin()即可訪問初始元素,如下所示:

std::cout << *(uvVertices.begin()); // Get the initial element

如果我理解正確...您可以在循環內將迭代器取消引用為臨時引用,以方便使用,並像訪問任何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);
}

您還可以將范圍用於:

for(auto& v  : uvVertices) {
    std::cout << v(0,0);  
    std::cout << v(1,0);
}

我也考慮將Eigen::Vector類型用於向量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM