C++ 使用std::nth_元素对arma::mat的行进行排序

C++ 使用std::nth_元素对arma::mat的行进行排序,c++,overloading,swap,armadillo,nth-element,C++,Overloading,Swap,Armadillo,Nth Element,正如标题所提到的,我的目标是在犰狳矩阵上使用std::nth_元素,该矩阵包含一组d维的N个点,即Nxd矩阵。这些行表示一个d维点。 两行的比较应沿固定尺寸进行,即 当A(i,s)

正如标题所提到的,我的目标是在犰狳矩阵上使用std::nth_元素,该矩阵包含一组d维的N个点,即Nxd矩阵。这些行表示一个d维点。 两行的比较应沿固定尺寸进行,即

当A(i,s) 我现在的想法是使用Armadillo提供的列迭代器,并重载std::swap函数,以便与两个列迭代器和我想要的交换一起使用:

void swap(arma::mat::col_iterator& lhs, arma::mat::col_iterator& rhs)
{
    lhs->M->swap_rows(lhs->current_row, rhs->current_row);
}
这里我使用arma::mat中构建的swap_rows方法。对std::nth_元素的调用如下所示:

// Setting indices to indicate which submatrix should be sorted:
arma::uword first = nodes[currentNodeIndex].indexFirstElem;
arma::uword last  = nodes[currentNodeIndex].indexLastElem + 1;
arma::uword nth = (last - first) / 2;    
// Calling std::nth_element:
std::nth_element(points.begin_col(first), points.begin_col(nth), points.begin_col(last));
现在的问题是,当以上述方式重载swap时,我会遇到编译器错误:

    kd_tree.cpp: In function ‘void swap(double*&, double*&)’:
kd_tree.cpp:25:7: error: request for member ‘M’ in ‘* lhs’, which is of non-class type ‘double’
   25 |  lhs->M->swap_rows(lhs->current_row, rhs->current_row);
      |       ^
kd_tree.cpp:25:25: error: request for member ‘current_row’ in ‘* lhs’, which is of non-class type ‘double’
   25 |  lhs->M->swap_rows(lhs->current_row, rhs->current_row);
      |                         ^~~~~~~~~~~
kd_tree.cpp:25:43: error: request for member ‘current_row’ in ‘* rhs’, which is of non-class type ‘double’
   25 |  lhs->M->swap_rows(lhs->current_row, rhs->current_row);
我猜这些错误告诉我列迭代器无法访问它所操作的矩阵M?此外,我似乎无法访问迭代器所在的当前_行。 我试图查看,但是除了迭代器的存在以及如何使用
begin()
初始化它之外,没有关于该迭代器的实际接口的更多信息。同时,查看犰狳的实际代码也没有帮助我,因为我还没有找到
arma:mat::col_迭代器的定义

所以我的问题是,上面的错误告诉了我什么,我怎样才能解决这个问题?此外,如果您碰巧知道解决所述问题的更好方法,我们也将非常感谢。:)

之前,我尝试为arma::mat的行编写一个自定义随机访问操作符,但失败了,因为我无法重载
value\u type&operator*(){return a.row(I);}
as
a.row(I)
似乎返回一个临时arma::vec对象或其他对象


编辑:我应该提到,我曾尝试使用迭代器实现自己的迭代器,并根据自己的需要进行调整。上面的swap函数也是受这个代码片段启发而来的。

我不知道armadillo库,但您的错误消息似乎表明arma::mat::col_迭代器是一个double*的typedef

您的目标是交换迭代器指向的值。 让我们尝试通过一个简单的独立测试来重现它:

#include <iostream>
#include <algorithm>

int main() {
    double* a = new double[2];
    double* b = new double[2];

    a[0] = 1.0;
    a[1] = 2.0;
    b[0] = 10.0;
    b[1] = 20.0;

    std::swap(a,b);
    std::cout << a[0] << "," << a[1] << std::endl;
    std::cout << b[0] << "," << b[1] << std::endl;    
    delete[] a;
    delete[] b;
    return 0;
}
因此,我将您的交换函数修改为:

void swap(arma::mat::col_iterator& lhs, arma::mat::col_iterator& rhs)
{
    std::swap(*lhs,*rhs);
}

更新,重新阅读问题我不确定您是否要交换元素或整行,在任何情况下,您都可以使用正确的交换函数进行交换。

我认为您误解了迭代器的工作原理,谢谢您的回答!我的目标是在只比较两行的第i个条目之后交换整行的内容(而不仅仅是这些行的第i个条目)。因此,简单地使用
交换(arma::mat::col_iterator&lhs,arma::mat::col_iterator&rhs)
在我的例子中不起作用,因为它只交换第i个条目。我的问题是,我不知道如何在这里以正确的方式修改swap。
void swap(arma::mat::col_iterator& lhs, arma::mat::col_iterator& rhs)
{
    std::swap(*lhs,*rhs);
}