繁体   English   中英

c ++如何搜索向量中的struct元素是否相等?

[英]c++ How to search if struct element in vector is equal?

名为SparseMatrix的类具有Node结构的向量。 我想重载+=运算符,以便如果Node实例的ij成员相同,则将该节点的值添加到This 如何使用算法库中的方法完成此操作?

我尝试使用find_if传递给函数,但它仅作用于一个迭代器:

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()传递两个参数find_ifitOther传递的当前迭代器,以便它可以检查它们是否相等。

编辑:我已经分离了samePosition函数,现在想使用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;
}

您正在尝试使用

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使这一切变得更加容易,因为lambda会导致编译器为您生成该结构:

itThis = find_if(_matrix.begin(), _matrix.end(), [itOther](Node& arg){ return ((itOther->i == arg.i) && (itOther->j == arg.j)); });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM