0

我有一个接受向量并修改它的函数。如何将 matrix_row 实例传递给此函数?我不想做任何复制操作

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/vector.hpp>
using namespace boost::numeric::ublas;

void useMatrixRow(matrix_row<matrix<double> >& mRow) {
//  ...
}
void useConstVector(const vector<double>& v) {
//  ...
}
void useVector(vector<double>& v) {
//  ...
}
void useMatrix(matrix<double>& m) {
    matrix_row<matrix<double> > mRow(m, 0);
    useMatrixRow(mRow); // works
    useConstVector(mRow); // works
    // useVector(mRow); // doesn't work
}

取消注释 useVector(mRow) 表达式时,我得到:

error: invalid initialization of reference of type 'boost::numeric::ublas::vector<double>&' from expression of type 'boost::numeric::ublas::matrix_row<boost::numeric::ublas::matrix<double> >'
src/PythonWrapper.cpp:60:6: error: in passing argument 1 of 'void useVector(boost::numeric::ublas::vector<double>&)'
4

1 回答 1

1

您可以制作useVector一个模板功能:

template<class T>
void useVector(T &v) {
...
}

或者(如果可能),传递迭代器而不是整个容器:

template<class IterT>
void useVector(IterT begin, IterT end) {
...
}
于 2013-12-22T13:12:22.807 回答