How to check/find an item in Dequeue using find() method
find() function finds the element in the given range of numbers. Returns an iterator to the first element in the range [first, last) that compares equal to the value to be searched. If no such element is found, the function returns last.
Syntax:
InputIterator find (InputIterator first, InputIterator last, const T& val)
Parameters:
first, last : Input iterators to the initial and final positions in a sequence.The range searched is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
val: Value to be search in the range.
Return Value:
An iterator to the first element in the range that compares equal to val.
If no elements match, the function returns last.
Example:
Input: 10 20 30 40
Output: Element 30 found at position : 2 (counting from zero)Input: 8 5 9 2 7 1 3 10
Output: Element 4 not found.
Example 1: Below is the C++ program to implement find() function in deque.
C++
// C++ program to implement // the above approach #include <iostream> #include<bits/stdc++.h> using namespace std; // Function to find element // in a deque void find(deque< int > q) { deque< int >::iterator itr; itr = find(q.begin(), q.end(), 2); if (itr != q.end()) { cout << "Found" ; } else { cout << "Not Found" ; } } // Driver code int main() { // Declaring a deque deque< int > q; // Initializing deque q.push_back(1); q.push_back(2); q.push_back(3); q.push_back(4); q.push_back(5); // Calling function find() find(q); return 0; } |
Output:
Found
Example 2: Below is a C++ program to demonstrate how to find elements in dequeue.
C++
// C++ program to implement // the above approach #include <iostream> #include<bits/stdc++.h> using namespace std; // Function to find an element // in a deque void find(deque<string> q) { deque<string>::iterator itr; itr = find(q.begin(), q.end(), "Raj" ); if (itr != q.end()) { cout << "Found" ; } else { cout << "Not Found" ; } } // Driver code int main() { // Declaring a deque deque<string> q; // Initializing a deque q.push_back( "Akshit" ); q.push_back( "Nikita" ); q.push_back( "Deeksha" ); q.push_back( "Nandish" ); q.push_back( "Rajat" ); // Calling find() function find(q); return 0; } |
Output:
Not Found
std::find() in C++ vs find in Deque
In std::find() in C++, the range searched is [first, last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
In the case of finding an item in deque using find() function, the range searched is [first, last] i.e both first and last inclusive.
Below is the C++ program to demonstrate finding an item in dequeue.
C++
// C++ program to implement // the above approach #include <iostream> #include<bits/stdc++.h> using namespace std; void find(deque< int > q) { deque< int >::iterator itr; itr = find(q.begin(), q.end(), 5); if (itr != q.end()) { cout << "Found" ; } else { cout << "Not Found" ; } } // Driver code int main() { // Declaring a deque deque< int > q; // Initializing deque q.push_back(1); q.push_back(2); q.push_back(3); q.push_back(4); q.push_back(5); // Calling find() function find(q); return 0; } |
Output:
Found
Please Login to comment...