我想扩展 C++ Eigen 库以包含命令 v.sort();我正在使用概述的基于 EIGEN_MATRIXBASE_PLUGIN 的方法 here .
下面的代码(在我的“MatrixBaseAddons.h”中)不起作用,因为“结果”对象没有加载“this”的拷贝——在调试器中,“result.rows()”是一个未初始化的值,不等于 derived()->rows()。我如何实际复制“this”并将其放入“result”?
// DOES NOT WORK
MatrixBase<Derived> sort(bool ascending = true) const {
MatrixBase<Derived> result = derived();
result.sortInPlace(ascending);
return result;
}
// WORKS!
void sortInPlace(bool ascending = true) {
std::sort(derived().data(), derived().data() + derived().size());
if (!ascending)
this->reverseInPlace();
}
最佳答案
MatrixBase
是一个抽象类。您需要返回一个 Matrix<>
具有适当标量类型和大小的对象。您可以直接使用 typedef PlainObject
为此:
PlainObject sort(bool ascending = true) const {
PlainObject res = derived();
...
return res;
}
关于c++ - 扩展 Eigen 库以包含排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40223829/