Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9146691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:48:30+00:00 2026-06-17T10:48:30+00:00

A class named SparseMatrix has a vector of Node s struct. I want to

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:48:31+00:00Added an answer on June 17, 2026 at 10:48 am

    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)); });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class named Info where Info has a string type instance variable
I have a class named CollectionPager which has a collection inside that of type
I've got a class named BackgroundWorker that has a thread constantly running. To turn
I have a div class named switch control which has a div class named
I have a Class named Question and a related Class named Answers. I want
I have a Class named Constants that contains all the constant variable in my
I have a class named Item . It has the following parameters: User owner
I have a class named Announcement , it has a Title ( NSString ),
I have a class named Foo that extends a class named Bar that extends
Say I have a class named Base and a class that derives from it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.