C++ c++;-找到你的条件?
如何检查C++ c++;-找到你的条件?,c++,C++,如何检查find\u是否找到匹配项?当我尝试以下代码时: SparseMatrix& SparseMatrix::operator+=(const SparseMatrix &other) { vector<Node>::iterator itThis; for (vector<Node>::const_iterator itOther = other._matrix.begin(); itOther != other._matrix.en
find\u是否找到匹配项?当我尝试以下代码时:
SparseMatrix& SparseMatrix::operator+=(const SparseMatrix &other)
{
vector<Node>::iterator itThis;
for (vector<Node>::const_iterator itOther = other._matrix.begin(); itOther != other._matrix.end(); ++itOther)
{
itThis = find_if(_matrix.begin(), _matrix.end(), position_finder(*itOther));
if(*itThis)
{
itThis->value += itOther->value;
} else
{
_matrix.push_back(*itOther);
}
}
return *this;
}
我知道它是一个常数,所以我不能改变它的值,但我想知道是否有匹配。来自:
find_if(先输入,后输入,一元谓词p)
返回值
迭代到满足条件的第一个元素,或最后一个
(如果未找到此类元素)
如果将迭代器返回到容器中的元素或end()
,请参阅
您可以将它与matrix.end()进行比较
如果未找到项,则find\u If
返回等于\u matrix.end()的迭代器
这就是其中之一。
could not convert ‘itThis.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Node*, _Container = std::vector<Node>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Node&]()’ from ‘Node’ to ‘bool’
if( itThis != _matrix.end())
{
}
else
{
}
if (itThis == _matrix.end()) { ... }