How to check / find if an item is in DEQUE

In the above code, the else-if part gives me an error. Else-if: else value, if x is not in deque, then ...

#include <iostream>
#include <ctime>
#include <stack>
#include <deque>
#include <algorithm>
deque<char> visited;
char x;

   if (x==target[4][4])
   {
           visited.push_back(x);            
           return (visited);
   }
   else if (!(find(visited.begin(), visited.end(), x)))
   {
       visited.push_back(x);
   }

ERROR : no operator! matches these operands

+5
source share
1 answer

If std::findit cannot find a specific value, it will return the "end" of the iterator pair.

else if (std::find(visited.begin(), visited.end(), x) == visited.end())
{
   // process the case where 'x' _is_not_ found between
   // visited.begin() and visited.end()

Edit: if you want to know if there is x in deque, just change the condition.

else if (std::find(visited.begin(), visited.end(), x) != visited.end())
{
   // process the case where 'x' _is_ found between
   // visited.begin() and visited.end()

Edit: If you are not familiar with the concept of iterators in C ++, read Understanding Iterators in STL .

+16
source

All Articles