-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator (stl).cpp
52 lines (43 loc) · 900 Bytes
/
iterator (stl).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include<list>
using namespace std;
template<typename T>
int search(T arr[],int n,T key){
for(int p=0;p<n;p++){
if(arr[p]==key){
return p;
}
}
//return n if element not found
return n;
}
template<class ForwardIterator,class T>
ForwardIterator search(ForwardIterator start,ForwardIterator end, T key){
while(start!=end){
if(*start==key){
return start;
}
start++;
}
return end;
}
int main() {
/*
int arr[] = {1,3,5,7,10,12};
int n = sizeof(arr)/sizeof(int);
cout<<search(arr,n,10)<<endl;
*/
list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(5);
l.push_back(3);
auto it = search(l.begin(),l.end(),50);
if(it==l.end()){
cout<<"element not present";
}
else{
cout<<*it<<endl;
}
return 0;
}