一元函数对象:
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
//一元函数对象,可以作为STL函数的参数
struct absInt {
int operator() (int val) { //一元函数操作符
return val < 0 ? -val : val;
}
};
//模板函数
template<typename elementType>
void FuncDispalyElement(const elementType& element)
{
cout << element << ' ';
}
//模板类,可以通过成员变量保持状态
template<typename elementType>
struct DisPlayElement
{
int m_nCount;
DisPlayElement()
{
m_nCount = 0;
}
void operator() (const elementType& element)
{
++m_nCount;
cout << element << ' ';
}
};
int main()
{
int i = -42;
absInt absObj;
unsigned int ui = absObj(i);
cout << ui << endl;
cout << "Hello 函数对象!" << endl;
vector<int> a;
for (int n = 0; n < 10; ++n)
a.push_back(n);
list<char> b;
for (char c = 'a'; c < 'k'; ++c)
b.push_back(c);
//STL算法
DisPlayElement<int> mResult;
mResult = for_each(a.begin(), a.end(), /*匿名函数对象DisPlayElement<int>()*/mResult);
cout << endl;
cout << "数量:" << mResult.m_nCount << endl;
for_each(b.begin(), b.end(), DisPlayElement<char>());
cout << endl;
return 0;
}
一元谓词:
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
//模板类,可以通过成员变量保持状态
template<typename numberType>
struct IsMutiple
{
numberType m_Divisor;
IsMutiple(const numberType& divisor)
{
m_Divisor = divisor;
}
void operator() (const numberType& element) const
{
return ((element%m_Divisor)==0);
}
};
int main()
{
vector<int> vecIntegers;
for (int i = 25; i < 100; ++i)
vecIntegers.push_back(i);
IsMutiple<int> a(4);
vector<int>::iterator iElement;
iElement=find_if(vecIntegers.begin(),vecIntegers.end(),a/*IsMutiple<int>(4)*/);
if(iElement!=vecIntegers.end())
{
cout<<"第一个4的整数倍的数是:"<<*iElement<<endl;
}
return 0;
}
二元函数对象:
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
//模板类,可以通过成员变量保持状态
template<typename elementType>
class CMultiply
{
public:
elementType operator() (const elementType& elem1, elementType& elem2)
{
return elem1*elem2;
}
};
int main()
{
vector<int> a,b;
for(int i=0;i<10;++i)
a.push_back(i);
for(int j=0;j<10;++j)
b.push_back(j);
vector<int> vecResult;
vecResult.resize(10);
transform(a.begin(),a.end(),b.begin(),vecResult.begin(),CMultiply<int>());
for(size_t nIndex=0;nIndex<vecResult.size();++nIndex)
cout<<vecResult[nIndex]<<' ';
cout<<endl;
return 0;
}
二元谓词:
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <string>
using namespace std;
class CCompareStringNoCase
{
public:
bool operator() (const string& str1, const string& str2) const //这里的const不可缺少
{
string str1LowerCase;
str1LowerCase.resize(str1.size());
transform(str1.begin(), str1.end(), str1LowerCase.begin(), tolower);
string str2LowerCase;
str2LowerCase.resize(str2.size());
transform(str2.begin(), str2.end(), str2LowerCase.begin(), tolower);
return (str1LowerCase < str2LowerCase);
}
};
int main()
{
set<string,CCompareStringNoCase> names;
names.insert("Tina");
names.insert("Jim");
names.insert("Jack");
names.insert("Sam");
names.insert("Bob");
set<string, CCompareStringNoCase>::iterator iNameFound = names.find("jim");
if (iNameFound != names.end())
{
cout << "找到了:" << *iNameFound << endl;
}
else
{
cout << "没找到" << endl;
}
return 0;
}