Question

I am iterating through a vector, which consists of 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

which is correct, how can i access only the first or the second component? So that i get

0.12312

http://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html here is a reference but i could not figur eit out.

Was it helpful?

Solution

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:

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.

OTHER TIPS

If you would like to get the n-th element of a container, you can use std::next, like this:

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:

std::cout << *(uvVertices.begin()); // Get the initial element
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top