0

という名前のクラスには、 s structSparseMatrixのベクトルがあります。Node インスタンスのとのメンバーが同じ場合、そのノードの値が に追加されるように、演算子Nodeをオーバーロードしたいと考えています。アルゴリズム ライブラリのメソッドを使用してこれを達成するにはどうすればよいですか?+=ijThis

関数に渡すために使用しようとしfind_ifましたが、1 つの反復子に対してのみ機能します。

class SparseMatrix
{
public:
    SparseMatrix(int numRow,int numCol, std::vector<double> fill);
    SparseMatrix(int numRow,int numCol);
    SparseMatrix();

    // assignment operations
    bool operator==(const SparseMatrix &other) const;
    bool operator!=(const SparseMatrix &other) const;
    void operator-() const;

    // compound operations
    SparseMatrix& operator+=(const SparseMatrix &other);
    SparseMatrix& operator*=(const SparseMatrix &other);

    // binary operations
    const SparseMatrix operator+(const SparseMatrix &other) const;
    const SparseMatrix operator*(const SparseMatrix &other) const;

    friend std::ostream& operator<<(std::ostream& output, const SparseMatrix sparseMatrix);

    bool trace(double& result) const;
    bool det(double& result) const;
    SparseMatrix transpose();

    ~SparseMatrix(){};


protected:
    vector<Node> _matrix;
    int _numCol, _numRow;
};

typedef struct Node {
    int i;
    int j;
    double value;
    static bool samePosition(const Node& other)
        {
            return ((i == other.i) && (j == other.j));
        }
} Node;




SparseMatrix& SparseMatrix::operator+=(const SparseMatrix &other)
{
    vector<Node>::iterator itThis;
    for (vector<Node>::iterator itOther = other._matrix.begin(); itOther != other._matrix.end(); ++itOther)
    {
            // find if already exists a value in the same matrix position
        itThis = find_if(_matrix.begin(), _matrix.end(), Node::samePosition);

            // if exists add value to position, else instantiate new Node with value &  position
    }

    return *this;
}

基本的に、私は Node::samePosition() に 2 つのパラメーターを渡したいと考えています。現在のイテレーターが渡されるので、それらが等しいかどうかを確認できますfind_ifitOther

編集:関数を分離し、次samePositionを使用して2つのパラメーターを渡したいと思いますfind_if:

typedef struct Node {
    int i;
    int j;
    double value;
} Node;

static bool SparseMatrix::samePosition(const Node& first, const Node& other)
{
    return ((first.i == other.i) && (first.j == other.j));
} 

SparseMatrix& SparseMatrix::operator+=(const SparseMatrix &other)
{
    vector<Node>::iterator itThis;
    for (vector<Node>::iterator itOther = other._matrix.begin(); itOther != other._matrix.end(); ++itOther)
    {
        itThis = find_if(_matrix.begin(), _matrix.end(), SparseMatrix::samePosition("call what here?",itOther));
    }

    return *this;
}
4

1 に答える 1

3

使用しようとしています

static bool SparseMatrix::samePosition(const Node& first, const Node& other)
{
    return ((first.i == other.i) && (first.j == other.j));
}

これはスタンドアロン機能です。そのすべてのデータは呼び出し元によって提供される必要がありますが、リスト全体と比較したいfind_ifについては何も知りません。Node

代わりに、いくつかのデータを保持できるオブジェクトでありoperator()()、関数のように呼び出すことができるように実装するファンクターを使用する必要があります。

struct position_finder
{
    const Node needle;
    position_finder( const Node& sought ) : needle(sought) {}
    bool operator()( const Node& haystack ) const
    {
        return ((needle.i == haystack.i) && (needle.j == haystack.j));
        // or return samePosition(needle, haystack)
    }
};

そして、ファンクターを構築するときに、検索されたノードを渡すので、後で使用するために保存されます。

itThis = find_if(_matrix.begin(), _matrix.end(), position_finder(*itOther));

C++11 では、ラムダによってコンパイラがその構造体を生成するため、これがすべて非常に簡単になります。

itThis = find_if(_matrix.begin(), _matrix.end(), [itOther](Node& arg){ return ((itOther->i == arg.i) && (itOther->j == arg.j)); });
于 2013-01-15T19:19:52.527 に答える