STL算法 函数对象 与 谓词
#include <iostream>
#include <algorithm>
#include <vector>
#include <algorithm>
#include <vector>
using namespace std;
//函数对象
class Print
{
public:
void operator()(int &val)
{
cout << "val=" << val << endl;
}
};
class Print
{
public:
void operator()(int &val)
{
cout << "val=" << val << endl;
}
};
//一元谓词
class Less
{
public:
bool operator()(int &value)
{
if (value %4 == 0)
{
return true;
}
else
{
return false;
}
}
};
class Less
{
public:
bool operator()(int &value)
{
if (value %4 == 0)
{
return true;
}
else
{
return false;
}
}
};
//函数对象的使用
void test_one()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i + 10);
}
Less less;
void test_one()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i + 10);
}
Less less;
vector<int>::iterator it = v.begin();
it = find_if(v.begin(), v.end(), less);
while (it != v.end())
{
cout << "*it:" << *it << endl;
it++;
if (it != v.end())
{
it = find_if(it, v.end(), less);
}
}
it = find_if(v.begin(), v.end(), less);
while (it != v.end())
{
cout << "*it:" << *it << endl;
it++;
if (it != v.end())
{
it = find_if(it, v.end(), less);
}
}
}
class Add
{
public:
int operator()(int value1, int value2)
{
return value1 + value2;
}
};
//函数对象 两个参数
void test_two()
{
vector<int> v1;
vector<int> v2;
vector<int> v3;
{
public:
int operator()(int value1, int value2)
{
return value1 + value2;
}
};
//函数对象 两个参数
void test_two()
{
vector<int> v1;
vector<int> v2;
vector<int> v3;
for (int i = 0; i < 10; i++)
{
v1.push_back(i + 1);
v2.push_back(i + 2);
}
Add add;
vector<int>::iterator it1 = v1.begin();
vector<int>::iterator it2 = v2.begin();
for (it1, it2; it1 != v1.end() && it2 != v2.end(); it1++, it2++)
{
v3.push_back(add(*it1, *it2));
}
{
v1.push_back(i + 1);
v2.push_back(i + 2);
}
Add add;
vector<int>::iterator it1 = v1.begin();
vector<int>::iterator it2 = v2.begin();
for (it1, it2; it1 != v1.end() && it2 != v2.end(); it1++, it2++)
{
v3.push_back(add(*it1, *it2));
}
for (vector<int>::iterator it = v3.begin(); it != v3.end(); it++)
cout << "*it=" << *it << endl;
cout << "*it=" << *it << endl;
}
void print(int &val)
{
cout << val << endl;
}
{
cout << val << endl;
}
//二元谓词
bool Compare(const int &a, const int &b)
{
return a < b;
}
bool Compare(const int &a, const int &b)
{
return a < b;
}
void test_three()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(rand() % 100);
}
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(rand() % 100);
}
for_each(v.begin(), v.end(), print);
sort(v.begin(), v.end(), Compare);
cout << "----------" << endl;
for_each(v.begin(), v.end(), print);
}
sort(v.begin(), v.end(), Compare);
cout << "----------" << endl;
for_each(v.begin(), v.end(), print);
}
int main(void)
{
test_one();
cout << "------" << endl;
test_two();
cout << "------" << endl;
test_three();
system("pause");
return 0;
}
{
test_one();
cout << "------" << endl;
test_two();
cout << "------" << endl;
test_three();
system("pause");
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
void bind_test()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
int num = count(v.begin(), v.end(), 3);
cout << "等于三的个数:" << num << endl;
cout << "等于三的个数:" << num << endl;
num = count_if(v.begin(), v.end(), bind2nd(less<int>(), 3));
cout << "小于三的个数:" << num << endl;
}
int add100(int val)
{
return val + 100;
}
void print(int val)
{
cout << "val=" << val << " ";
}
{
return val + 100;
}
void print(int val)
{
cout << "val=" << val << " ";
}
void tranfrom_test()
{
vector<int> v1;
vector<int> v2;
vector<int> v3;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i + 1);
}
{
vector<int> v1;
vector<int> v2;
vector<int> v3;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i + 1);
}
for_each(v1.begin(), v1.end(), print);
cout << endl;
cout << "-------------" << endl;
for_each(v2.begin(), v2.end(), print);
cout << "-----------" << endl;;
cout << endl;
cout << endl;
cout << "-------------" << endl;
for_each(v2.begin(), v2.end(), print);
cout << "-----------" << endl;;
cout << endl;
v3.resize(v1.size());
transform(v1.begin(), v1.end(), v3.begin(),add100);
for_each(v3.begin(), v3.end(), print);
}
transform(v1.begin(), v1.end(), v3.begin(),add100);
for_each(v3.begin(), v3.end(), print);
}
int main(void)
{
//bind_test();
{
//bind_test();
tranfrom_test();
system("pause");
return 0;
}
}