Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
nonmutating cpp #pragma warning (disable : 4786) ,vector example       Share
2008-09-28 |  JordenTiger  | Viewed: 238  |    0

#pragma warning (disable : 4786)

#include <iostream>
#include <vector>
#include <iterator>
#include <functional>
#include <algorithm>
using namespace std;


const 
int VectorSize = 10;

template<class T>

class Print: public unary_function<T, 
void>
{

public:

void operator()(T& arg1)
{

cout << arg1 << " ";

}

};


template<class T>

class GreaterThanTwo: public unary_function<T, bool>

{

public:

bool operator()(T& arg1)

{

return (arg1 > 2);

}

};


template<class T>

class WithinOne: public binary_function<T, T, bool>

{

public:

bool operator()(T& arg1, T& arg2)

{

return (abs(arg2 - arg1) <= 1);

}

};



template<class Container, class Iterator>

void SequenceRange(Container& c, Iterator& first, Iterator& last);

template<class Container, class Iterator>

void ShowElement(Container& c, Iterator& itor);

int main()
{

Print<int> DoPrint;

vector<int> vInt(VectorSize);

typedef vector<int>::iterator Itor;


for (int i = 0; i < VectorSize; ++i)
vInt[i] = i + i / 3;


Itor first = vInt.begin();

Itor last = vInt.end();


cout << "
for_each()\n";
for_each(first, last, DoPrint);
cout << "\n";


Itor retItor = find(first, last, 2);

cout << "find(first, last, 2) = ";

ShowElement(vInt, retItor);

cout << "\n";


retItor = find(first, last, 10);

cout << "find(first, last, 10) = ";

ShowElement(vInt, retItor);

cout << "\n";


GreaterThanTwo<int> IsGreaterThanTwo;

retItor = find_
if(first, last, IsGreaterThanTwo);
cout << "find(first, last, IsGreaterThanTwo) = ";

ShowElement(vInt, retItor);

cout << "\n";


int retSize = count(first, last, 3);
cout << "count_
if(first, last, 3) = " << retSize << "\n";

retSize = count_
if(first, last, IsGreaterThanTwo);
cout << "count_
if(first, last, IsGreaterThanTwo) = " << retSize << "\n";

retItor = adjacent_find(first, last);

cout << "adjacent_find(first, last) = ";

ShowElement(vInt, retItor);

cout << "\n";


WithinOne<int> IsWithinOne;

retItor = adjacent_find(first, last, IsWithinOne);

cout << "adjacent_find(first, last, IsWithinOne) = ";

ShowElement(vInt, retItor);

cout << "\n";


vInt[3] = 2;

retItor = adjacent_find(first, last);

cout << "adjacent_find(first, last) = ";

ShowElement(vInt, retItor);

cout << "\n";


return 0;

}


template<class Container, class Iterator>

void ShowElement(Container& c, Iterator& itor)
{

if (itor != c.end())
cout << *itor;

else

cout << "last";

}


template<class Container, class Iterator>

void SequenceRange(Container& c, Iterator& first, Iterator& last)
{

first = c.begin();

last = c.end();

}


Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software
Tech: C ,Cpp
Send Mail: malathi.v



Related topics
Stack Implementation
cpp stack example

General Topics
automate functional testing process
automated testing
qtp - regular expression example
qtp : vbscript to create a file
qtp data tables
qtp page checkpoint
qtp interview questions
great art

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us