3.(templates and iterator: 15pts) Write a templated function find that take type
ID: 3598982 • Letter: 3
Question
3.(templates and iterator: 15pts) Write a templated function find that take type (the type for begin and end should template para the standard interface for iterators. parameter. The function should search in the sequence pointe begin and exclusive of end s in three arguments: begin and end are iterators of the same be a template parameter). You may assume begin and end have The third argument is value, whose type should also be a template d to from begin until end, (inclusive of , and look for one that is equal to value. It returns an iterator pointing to the at is equal to value, or an iterator that equals end if none is foundExplanation / Answer
template <class RandomAccessIterator, class T>
RandomAccessIterator find1( RandomAccessIterator begin, RandomAccessIterator end, const T& value )
{
while( begin != end ) {
if( *begin == value ) {
return begin;
}
++begin;
}
return end;
}
/*for understanding you can see this c++ code in case of vector*/
/*
#include<bits/stdc++.h>
using namespace std;
template <class RandomAccessIterator, class T>
RandomAccessIterator find1( RandomAccessIterator begin, RandomAccessIterator end, const T& value )
{
while( begin != end ) {
if( *begin == value ) {
return begin;
}
++begin;
}
return end;
}
int main()
{
vector<int> v = { 12, 23, 44, 102, 1 };
vector<int>::iterator b=find1( v.begin(), v.end(), 44 );
cout<<*b<<endl;
}
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.