Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in AI and Deep Learning by (50.2k points)

In the code above the else-if part gives me an error. The meaning of else-if is: else if the value of x isn't in the 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

1 Answer

0 votes
by (108k points)

Use std::find() method:

numbers.push_back(10); 

numbers.push_front(20); 

numbers.push_back(30); 

numbers.push_front(40); 

deque<int>::iterator it = find(numbers.begin(), numbers.end(), 20); if(it!=numbers.end()) 

{ // Do your stuff. Here I am simply deleting the element 

it = numbers.erase(it); // Note: Always save returned iterator from erase/insert method, otherwise 

// iterator will point to deleted resource, which leads to undefined behaviour. 

}

If std::find cannot find the 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

If you want to know if x is in the deque, just reverse 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()

What happens if x has the same value as visited.end()?

Simply use != if you want to know if x is in the deque as it cannot. .end() points to the location after the back of the deque.

For more information, you can refer the following link:

http://www.cplusplus.com/reference/deque/deque/at/

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.2k users

Browse Categories

...