-2
\$\begingroup\$

I need to know how to check if a element (object) inside a vector exists.

I've heard of std::find but the I don't understand how to use it (even though I've read multiple answered questions here, and on other forums).

find(_InIt _First, _InIt _Last, const _Ty& _Val)
  • What are all the parameters?

  • What does the function return?

I couldn't find any good posts that explain this (or at least, didn't explain it good enough for me to understand).


Example : (this is the return type)

An iterator to the first element in the range that compares equal to val. If no elements match, the function returns last.

(taken from std::find documentation) This makes no sense at all to me.


Examaple of how I would like to use it :

if (texts[index]) == does exist)
    do something
else
    do something else

This is how I'm currently using it, which is wrong.

std::vector<Text*>::iterator result;
result = find(texts.begin(), texts.end(), texts.begin() + index);

if (result != texts.end())
    std::cout << "Element removed" << std::endl;
else
    std::cout << "Element not found in texts, won't remove." << std::endl;

It gives me this error : (just parts of it, scroll down if you need the entire error...)

error C2679: binary '==': no operator found which takes a right-hand operand of type 'const std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Text *>>>' (or there is no acceptable conversion)

Full error :

1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\system_error(379): note: could be 'bool std::operator ==(const std::error_condition &,const std::error_condition &) noexcept'
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\system_error(371): note: or       'bool std::operator ==(const std::error_condition &,const std::error_code &) noexcept'
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\system_error(363): note: or       'bool std::operator ==(const std::error_code &,const std::error_condition &) noexcept'
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\system_error(355): note: or       'bool std::operator ==(const std::error_code &,const std::error_code &) noexcept'
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\exception(341): note: or       'bool std::operator ==(const std::exception_ptr &,std::nullptr_t) throw()'
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\exception(336): note: or       'bool std::operator ==(std::nullptr_t,const std::exception_ptr &) throw()'
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\exception(331): note: or       'bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &) throw()'
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(3134): note: while trying to match the argument list '(Text *, const std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Text *>>>)'
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(3153): note: see reference to function template instantiation '_InIt std::_Find<_InIt,_Ty>(_InIt,_InIt,const _Ty &,std::false_type)' being compiled
1>          with
1>          [
1>              _InIt=Text **,
1>              _Ty=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Text *>>>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(3162): note: see reference to function template instantiation '_InIt std::_Find<Text**,_Ty>(_InIt,_InIt,const _Ty &)' being compiled
1>          with
1>          [
1>              _InIt=Text **,
1>              _Ty=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Text *>>>
1>          ]
1>  c:\users\myname\desktop\c++ projects\sfml_game\sfml_game\main.cpp(93): note: see reference to function template instantiation '_InIt std::find<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Text *>>>,std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Text *>>>>(_InIt,_InIt,const _Ty &)' being compiled
1>          with
1>          [
1>              _InIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Text *>>>,
1>              _Ty=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Text *>>>
1>          ]

Edit, updated my code, now looks like this but when I run it, it prints "Could not remove element, it does not exist!".

void RemoveText(unsigned int index)
{
    std::vector<Text*>::iterator result;
    result = find(texts.begin(), texts.end(), texts[index]);

    if (result >= texts.end())
        texts.erase(texts.begin() + index); 
    else
        std::cout << "Could not remove element, it does not exist!" << std::endl;

}
\$\endgroup\$
1
\$\begingroup\$

This should work

std::vector<Text*>::iterator result;
result = find(texts.begin(), texts.end(), thing_I_want);

Explanation of iterators

texts.begin() returns an iterator which points to the first element in text. Iterators have a special property, that adding a number to them will increment the index they reference.

More clearly, *(texts.begin()) is the first element of texts. *(texts.begin() +1) is the second element of texts. *(texts.end()) is the last element of texts.

Explanation of std::find

result = std::find(

arg1 // iterator pointing to where we should start searching

arg2 // Iterator pointing to where we should stop searching

arg3 // Thing to search for in container

)

It will return another iterator, this one pointing to the element that matched. If you'd like to recover the index, you can do either

int index = std::distance(Texts.begin(), result)

Or

int index = result - Texts.begin()

What went wrong

When you did index + texts.begin(), the result of the addition was an iterator, for which there isn't a comparison operation to your text type. That makes sense, because the third argument to std::find is what we're looking for, so we (in the worst case) need to compare that to all of the elements in the range we're searching. If we don't have a defined comparison between the thing we're searching for, and the elements of our vector, then there must be no way we can search for it.

\$\endgroup\$
  • \$\begingroup\$ Of what type is arg3? What should I write in "thing_i_want" ? \$\endgroup\$ – BiiX Jan 3 '16 at 19:41
  • \$\begingroup\$ You are searching for something, what are you searching for? \$\endgroup\$ – Jacob Panikulam Jan 3 '16 at 21:34
  • 1
    \$\begingroup\$ Oh, I think I see - you aren't searching for anything, you just want to see if it exists. The only case where a vector element "doesn't exist" is when index >= vector.size(). Otherwise, there must be something there, even if it is a NULL ptr. \$\endgroup\$ – Jacob Panikulam Jan 3 '16 at 21:37
  • \$\begingroup\$ Yes, that's exactly what I want to do. I want to check if a specific element is exists inside my vector. \$\endgroup\$ – BiiX Jan 3 '16 at 22:46
  • \$\begingroup\$ Added the "updated" version of my code. \$\endgroup\$ – BiiX Jan 4 '16 at 0:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.