更快地通过std :: map工作的方法

时间:2017-05-25 12:02:15

标签: matrix stdmap

我遇到了问题。我使用这样的地图运行程序:

template<typename T>
class Matrix{
public:
    //--- Parameters ---
    std::map<std::array<int,2>,T> data;
    const int nRow;
    const int nCol;

这适用于所有方法和运营商。 到目前为止,我正在为运算符使用for循环,例如:

Matrix& operator+=(const Matrix<T>& other){
for (auto r = 0; r<nRow;r++){
    for (auto c= 0; c<nCol;c++){
    if(other.data.count({r,c}))
            data[{r,c}] +=other[{r,c}];
};
};
return *this;
};

Matrix operator+(const Matrix<T>& other){
    return Matrix(*this)+=other;
};

不幸的是,如果矩阵变得非常庞大,这种方法是超级的。真的很慢。所以我尝试了std::transform之类的东西等等。 但不幸的是,我无法让它在地图结构中工作。您是否有想法让它工作或加快速度?这真的需要很长时间,我是Cpp的新手,所以我想学习如何以正确的方式做到这一点。

非常感谢:)

0 个答案:

没有答案