Domanda

A class named SparseMatrix has a vector of Nodes struct. I want to overload the += operator so that if the i and j members of a Node instance are the same then the value of that node will be added to This. How can i accomplish this using methods from the algorithm library?

I tried using find_if to pass to a function but it act only on one iterator:

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;
}

Basically, I want Node::samePosition() to pass two parameters - the current iterator passed by find_if and itOther so it can check if they are equal.

EDIT: I have separated the samePosition function and now want to pass to it two parameters using 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;
}
È stato utile?

Soluzione

You are trying to use

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

which is a standalone function. All its data has to be supplied by the caller, but find_if knows nothing about the Node you want to compare against the entire list.

Instead you should use a functor, which is an object that can hold some data, and also implements operator()() so that it can be called like a function.

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)
    }
};

and then you pass the sought Node when constructing the functor, so it gets stored for later use:

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

C++11 makes this all a whole lot easier, since a lambda will cause the compiler to generate that struct for you:

itThis = find_if(_matrix.begin(), _matrix.end(), [itOther](Node& arg){ return ((itOther->i == arg.i) && (itOther->j == arg.j)); });
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top