//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include <iostream>
#include <vector>
#include <algorithm> //needed for std::find
using namespace std;
bool isOdd(int i)
{
return ((i%2)==1);
}
int main()
{
vector<int> v1;
v1.push_back(88);
v1.push_back(99);
v1.push_back(22);
v1.push_back(33);
v1.push_back(44);
v1.push_back(55);
v1.push_back(66);
v1.push_back(77);
//Demonstrating Find - Find number 55 and its position, if present
vector<int>::const_iterator it = find(v1.begin(), v1.end(), 55);
if(it != v1.end())
{
cout<<"Found: "<<*it<<endl;
cout<<"Position = "<<it - v1.begin() + 1<<endl;
}
else
{
cout<<"Not Found"<<endl;
cout<<"Position = -1"<<endl;
}
//Demonstrating Find IF - Find the first Odd number and its position
it = find_if(v1.begin(), v1.end(), isOdd);
if(it != v1.end())
{
cout<<"Found: "<<*it<<endl;
cout<<"Position = "<<it - v1.begin() + 1<<endl;
}
else
{
cout<<"No Odd Numbers Found"<<endl;
cout<<"Position = -1"<<endl;
}
return 0;
}
The output is as follows:
No comments:
Post a Comment