title: c++ How to search if struct element in vector is equal?
body: <p>A class named <code>SparseMatrix</code> has a vector of <code>Node</code>s struct. I want to overload the <code>+=</code> operator so that if the <code>i</code> and <code>j</code> members of a Node instance are the same then the value of that node will be added to <code>This</code>. How can i accomplish this using methods from the algorithm library?</p>
  
  <p>I tried using <code>find_if</code> to pass to a function but it act only on one iterator: </p>
  
  <pre><code>class SparseMatrix
  {
  public:
      SparseMatrix(int numRow,int numCol, std::vector&lt;double&gt; fill);
      SparseMatrix(int numRow,int numCol);
      SparseMatrix();
  
      // assignment operations
      bool operator==(const SparseMatrix &amp;other) const;
      bool operator!=(const SparseMatrix &amp;other) const;
      void operator-() const;
  
      // compound operations
      SparseMatrix&amp; operator+=(const SparseMatrix &amp;other);
      SparseMatrix&amp; operator*=(const SparseMatrix &amp;other);
  
      // binary operations
      const SparseMatrix operator+(const SparseMatrix &amp;other) const;
      const SparseMatrix operator*(const SparseMatrix &amp;other) const;
  
      friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; output, const SparseMatrix sparseMatrix);
  
      bool trace(double&amp; result) const;
      bool det(double&amp; result) const;
      SparseMatrix transpose();
  
      ~SparseMatrix(){};
  
  
  protected:
      vector&lt;Node&gt; _matrix;
      int _numCol, _numRow;
  };
  
  typedef struct Node {
      int i;
      int j;
      double value;
      static bool samePosition(const Node&amp; other)
          {
              return ((i == other.i) &amp;&amp; (j == other.j));
          }
  } Node;
  
  
  
  
  SparseMatrix&amp; SparseMatrix::operator+=(const SparseMatrix &amp;other)
  {
      vector&lt;Node&gt;::iterator itThis;
      for (vector&lt;Node&gt;::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 &amp;  position
      }
  
      return *this;
  }
  </code></pre>
  
  <p>Basically, I want Node::samePosition() to pass two parameters - the current iterator passed by <code>find_if</code> and <code>itOther</code> so it can check if they are equal.</p>
  
  <p><strong>EDIT:</strong> I have separated the <code>samePosition</code> function and now want to pass to it two parameters using <code>find_if</code>:</p>
  
  <pre><code>typedef struct Node {
      int i;
      int j;
      double value;
  } Node;
  
  static bool SparseMatrix::samePosition(const Node&amp; first, const Node&amp; other)
  {
      return ((first.i == other.i) &amp;&amp; (first.j == other.j));
  } 
  
  SparseMatrix&amp; SparseMatrix::operator+=(const SparseMatrix &amp;other)
  {
      vector&lt;Node&gt;::iterator itThis;
      for (vector&lt;Node&gt;::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;
  }
  </code></pre>