在矢量向量中找到元素的位置

时间:2015-11-01 18:36:42

标签: c++ c++11 vector

A成为double类型向量的向量,即vector<vector<double> > AB相同但对于整数,即vector<vector<int> > B。假设AB的大小相同(并且每个嵌套向量的大小也相等)。

我想使用j检查i - B向量中是否包含std::find,所以我写std::find(B[i].begin(), B[i].end(), j) != B[i].end()。现在,如果是true,我想返回与A对应的[i][position_of_j_found_in_B[i]]中的值。我怎么能做到这一点?

我的代码如下:

    class SparseMatrix
    {
        private:
            vector<vector<double> > entries_;
            vector<vector<int> > columnIndices_;
        public:
            SparseMatrix();
            SparseMatrix(vector<vector<double> >,vector<vector<int> >);
            ~SparseMatrix();

            // getters
            vector<vector<double> > getEntries();
            vector<vector<int> > getColIndices();

            double operator()(const unsigned int&, const unsigned int&);

            vector<int> size();

    };

    SparseMatrix::SparseMatrix() { };

    SparseMatrix::SparseMatrix(vector<vector<double> > values, vector<vector<int> > colInd) : entries_(values), columnIndices_(colInd) { };

    SparseMatrix::~SparseMatrix() { };

    vector<vector<double> > SparseMatrix::getEntries() { return entries_; }

    vector<vector<int> > SparseMatrix::getColIndices() { return columnIndices_; }

    double SparseMatrix::operator()(const unsigned int i, const unsigned int j)
    {
        assert(i <= (*this).size()[0] && j <= (*this).size()[1]);

        auto it = find(columnIndices_[i].begin(),columnIndices_[i].end(), j);

        if(it != columnIndices_[i].end())
            return entries_[i][it - columnIndices_[i].begin()];
        return 0.0;
    }

vector<int> SparseMatrix::size() // returns dimensions of the matrix
{
    vector<int> dim(2); // stores dimensions of the matrix
    dim[0] = columnIndices_.size(); // numbers of rows in a matrix

    int temp = 0;
    vector<vector<int> >::iterator i;
    for(i=columnIndices_.begin(); i != columnIndices_.end(); ++i)
    {
        if(*max_element((*i).begin(),(*i).end()) > temp) // if maximal element in i-th vector of columnIndices_ is greater then current (temp)
            temp = *max_element((*i).begin(),(*i).end()); // update current biggest with new one
    }
    dim[1] = temp;
    return dim;
}

我的main()功能

int main(int argc, char const *argv[])
{
    vector<vector<double> > values {{3.0, 1.0}, {2.0}, {5.0, 4.0}};
    vector<vector<int> > columns {{1,3}, {1}, {2,3}};

    SparseMatrix A(values,columns);

    cout << "Matrix A has dimensions " << A.size()[0] << "x" << A.size()[1] << "." << endl; // works fine

    cout << A.getEntries()[0][0] << " " <<  A(0,0) << endl; // I expect the same output...
    return 0; 
}

1 个答案:

答案 0 :(得分:1)

这是解决原始问题的解决方案:

double foo(const vector<vector<double> >& X, const vector<vector<int> >& Y, unsigned int i, unsigned int j)
{
  auto  it = find(Y[i].begin(),Y[i].end(), j);

  if (it != Y[i].end())
      return X[i][it - Y[i].begin()];

  return 0.0;
}

P.S。我通过引用传递vector,因为为了优化,出于同样的原因我通过值传递unsigned int。

以下是SparseMatrix实施的示例:

#include <cassert>
#include <vector>
#include <tuple>
#include <iostream>
#include <cmath>
#include <algorithm>

class SparseMatrix final {
public:
    explicit SparseMatrix(const std::vector<double>& full_matrix) {
        static constexpr double EPS = 1e-16;
        const size_t N = std::sqrt(full_matrix.size());
        assert((N * N) == full_matrix.size());
        for (size_t i = 0; i < N; ++i)
            for (size_t j = 0; j < N; ++j)
                if (std::fabs(full_matrix[i * N + j]) > EPS)
                    data_.push_back(std::make_tuple(full_matrix[i * N + j], i, j));
    }
    double operator()(size_t i, size_t j) const {
        auto it = std::find_if(data_.begin(), data_.end(),
                       [&i, &j](const std::tuple<double, size_t, size_t>& elm) {
                           return std::get<1>(elm) == i && std::get<2>(elm) == j;
                 });
        return it == data_.end() ? 0.0 : std::get<0>(*it);
    }
private:
    std::vector<std::tuple<double, size_t, size_t>> data_;
};

int main()
{
    SparseMatrix A({3.0, 0, 1.0,
            2.0, 0, 0,
                0, 5.0, 4.0});
    std::cout << A(0, 0) << '\n';
}

请注意,此SparseMatrix适合学习项目,但对于每天使用, 为C ++使用已有的线性代数类的最佳方法。