Wednesday, July 30, 2008

C++ Source code for using STL unary_functions.

#include "stdafx.h"

#include
#include
#include
#include // for remove_if
#include
#include
using namespace std;

template
class is_vowel : public unary_function {

public:
bool operator ()(T t) const
{
if ((t=='a')||(t=='e')||(t=='i')||(t=='o')||(t=='u'))
return true; //t is a vowel
return false; // t is not a vowel
}
};

bool greaterthan(int x) {

return x>6;
}
int main()
{

string original;
cout << "Type string to remove vowels." <<>> original;
//…

string::iterator it= remove_if(original.begin(),
original.end(),
is_vowel());

// create new string from original using previous iterator
string no_vowels(original.begin(),it);
cout <<<><< "Remove all numbers greater than 6" < v1;
v1.push_back(7);
v1.push_back(1);
v1.push_back(9);
v1.push_back(2);
v1.push_back(0);
v1.push_back(7);
v1.push_back(7);
v1.push_back(3);
v1.push_back(4);
v1.push_back(6);
v1.push_back(8);
v1.push_back(5);
v1.push_back(7);
v1.push_back(7);

for(vector::iterator itr_prn = v1.begin() ;itr_prn!= v1.end(); itr_prn++)
cout<<" "<< *itr_prn ; cout <::iterator itre = remove_if(v1.begin(),v1.end(),greaterthan);
vector::iterator itrb = v1.begin();

for(;itrb != itre; itrb++)
cout << " " << *itrb;


}

No comments:

Your Title