c++ - boost :使用 matrix_row 代替 vector

标签 c++ boost ublas

我有一个函数接受一个 vector 并修改它。如何将 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>&)'

最佳答案

您可以使 useVector 成为模板函数:

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

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

template<class IterT>
void useVector(IterT begin, IterT end) {
...
}

关于c++ - boost :使用 matrix_row 代替 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20729286/

相关文章:

boost - 如何读取/转换 stanford bunny .ply-files 的范围图像?

boost - 如何创建一个简单的 boost::lambda 函数?

c++ - boost::smart_ptr 的内部结构

linear-regression - 权重变化时有效重新计算加权最小二乘回归

c++ - 如何使用 for 循环回到起点?

c++ - 如何在 C++ 中打印粗体字符串?

c++ - 使用指针或引用返回 Boost 矩阵有优势吗?

C++ Qt : QStyledItemDelegate's createEditor is never called, 尽管调用了 paint()

c++ - c变量在内存中的存储