How do you check if find_if has found a match or not? when i try the code below:
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;
}
I get at if(*itThis):
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’
I understand that itThis is a constant so i cant change it’s value but i want to know whether there was a match at all.
find_if returns iterator to either element in container or to
end(), see referenceYou could compare
itThiswith_matrix.end()