如何使用 find() 方法在 Dequeue 中检查/查找项目
find()函数在给定的数字范围内查找元素。返回一个迭代器,指向范围 [first, last) 中比较等于要搜索的值的第一个元素。如果没有找到这样的元素,则函数最后返回。
句法:
InputIterator find (InputIterator first, InputIterator last, const T& val)
参数:
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.
返回值:
An iterator to the first element in the range that compares equal to val.
If no elements match, the function returns last.
例子:
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.
示例 1:下面是在 deque 中实现 find()函数的 C++ 程序。
C++
// C++ program to implement
// the above approach
#include
#include
using namespace std;
// Function to find element
// in a deque
void find(deque q)
{
deque::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 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;
}
C++
// C++ program to implement
// the above approach
#include
#include
using namespace std;
// Function to find an element
// in a deque
void find(deque q)
{
deque::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 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;
}
C++
// C++ program to implement
// the above approach
#include
#include
using namespace std;
void find(deque q)
{
deque::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 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;
}
输出:
Found
示例 2:下面是一个 C++ 程序,用于演示如何在出队中查找元素。
C++
// C++ program to implement
// the above approach
#include
#include
using namespace std;
// Function to find an element
// in a deque
void find(deque q)
{
deque::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 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;
}
输出:
Not Found
C++ 中的 std::find() 与 Deque 中的 find
在C++ 的 std::find() 中,搜索的范围是 [first, last),它包含 first 和 last 之间的所有元素,包括 first 指向的元素,但不包括 last 指向的元素。
在使用 find()函数在 deque 中查找项目的情况下,搜索的范围是 [first, last],即 first 和 last 都包括在内。
下面是演示在出队中查找项目的 C++ 程序。
C++
// C++ program to implement
// the above approach
#include
#include
using namespace std;
void find(deque q)
{
deque::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 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;
}
输出:
Found