如何在Eigen中使用stl迭代器? [英] How can I use stl iterators with Eigen?
问题描述
我正在尝试在项目中使用Library Eigen,并且必须对向量进行排序.我尝试遵循该文档,它说该库应与STL迭代器和算法一起以可预测的方式工作
I am trying to use the Library Eigen in a project and I have to sort a vector. I tried to follow the documentation and it says that the library should work in the predictable way with the STL iterators and algorithms https://eigen.tuxfamily.org/dox-devel/group__TutorialSTL.html. However when I try to run the following test code
#include <iostream>
#include <algorithm>
#include <eigen3/Eigen/Dense>
#include <eigen3/Eigen/Core>
int main()
{
Eigen::Array4i v = Eigen::Array4i::Random().abs();
std::cout << "Here is the initial vector v:\n" << v.transpose() << "\n";
std::sort(v.begin(), v.end());
std::cout << "Here is the sorted vector v:\n" << v.transpose() << "\n";
return 0;
}
我收到以下两个错误:
error: ‘Eigen::Array4i’ {aka ‘class Eigen::Array<int, 4, 1>’} has no member named ‘begin’
9 | std::sort(v.begin(), v.end());
| ^~~~~
error: ‘Eigen::Array4i’ {aka ‘class Eigen::Array<int, 4, 1>’} has no member named ‘end’
9 | std::sort(v.begin(), v.end());
我用gcc 9.1.0和7.4.0进行了测试,我的Eigen版本是3.3.4.我正在使用Ubuntu 18.04,并且该库位于通常的位置/usr/include.我尝试过的所有其他功能似乎都能正常工作. 是众所周知的错误,是编译器问题还是版本问题?
I tested it with gcc 9.1.0 and 7.4.0 and my version of Eigen is 3.3.4. I am using Ubuntu 18.04 and the library is in the usual location /usr/include. All the other functionality I tried seem to work properly. Is it a well known bug, is it a compiler problem or is it a version problem?
推荐答案
如果您不想等待Eigen 3.4的发布,则可以使用以下方法:
If you don't want to wait for the release of Eigen 3.4, you can use this:
std::sort(v.data(), v.data() + v.size());
.data()
方法可以替换缺少的.begin()
,但是.end()
方法必须手动构造.
The .data()
method can replace the missing .begin()
, but the .end()
method must be constructed manually.
这篇关于如何在Eigen中使用stl迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!