class MyPrint
{
public:
void operator()(char *s)
{
cout<
#include
#include
#include
using namespace std;
class GreaterThan30
{
public:
bool operator()(int val)
{
return val>30;
}
};
bool greaterThan30(int val)
{
return val>30;
}
void test02()
{
vector v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
//寻找大于30的数
vector::iterator ret;
//仿函数提供策略 需要函数对象
//ret = find_if(v1.begin(), v1.end(), GreaterThan30() );
//普通函数提供策略
ret = find_if(v1.begin(), v1.end(), greaterThan30 );
if(ret != v1.end())
{
cout<<"找到的值:"<<*ret<
2.使用二元谓词实现排序
void myPrintInt(int val)
{
cout<v2;
}
};
bool myGreater(int v1, int v2)
{
return v1>v2;
}
void test03()
{
vector v1;
v1.push_back(30);
v1.push_back(20);
v1.push_back(10);
v1.push_back(40);
v1.push_back(50);
for_each(v1.begin(),v1.end(), myPrintInt);
cout<
语法
示例
#include
void test04()
{
plus pl;
int num01 = pl(10,20);
cout << "num01 = " << num01 << endl;
int num02 = plus()(1,20);
cout << "num02 = " << num02 << endl;
int num03 = minus()(1,20);
cout << "num03 = " << num03 << endl;
int num04 = multiplies()(1,20);
cout << "num04 = " << num04 << endl;
int num05 = divides()(20,5);
cout << "num05 = " << num05 << endl;
int num06 = modulus()(5,3);
cout << "num06 = " << num06 << endl;
int num07 = negate()(10);
cout << "num07 = " << num07 << endl;
}
class Staff{
friend void printStaff(Staff &s);
string name;
double money;
public:
Staff(){}
Staff(string name,double money)
{
this->name = name;
this->money = money;
}
};
void printStaff(Staff &s)
{
double money = plus()(s.money,2000);
cout << s.name << "本月工资为:" << money << endl;
}
void test05()
{
vector staffs;
staffs.push_back(Staff("张三",3100));
staffs.push_back(Staff("李四",3000));
staffs.push_back(Staff("王五",3200));
for_each(staffs.begin(),staffs.end(),printStaff);
}
语法
示例
class Staff{
string name;
double money;
public:
Staff(){}
Staff(string name,double money)
{
this->name = name;
this->money = money;
}
double getMoney()
{
return money;
}
};
class PlusMoney:public binary_function{
public:
void operator()(Staff s,double d) const
{
double money = s.getMoney() + d;
cout << money << " ";
}
};
void test08()
{
vector staffs;
staffs.push_back(Staff("张三",3100));
staffs.push_back(Staff("李四",3000));
staffs.push_back(Staff("王五",3200));
for_each(staffs.begin(),staffs.end(),bind2nd(PlusMoney(),2000.0));
}
void myPrintInt2(int val,int tmp)
{
cout< v1;
v1.push_back(30);
v1.push_back(20);
v1.push_back(10);
v1.push_back(40);
v1.push_back(50);
//1、bind2nd或bind1st 进行参数绑定
for_each(v1.begin(),v1.end(), bind1st(ptr_fun(myPrintInt2),300) );
cout<
class Data
{
private:
int a;
public:
Data(){}
Data(int a):a(a){}
void showData()
{
cout<a< v1;
v1.push_back(Data(10));
v1.push_back(Data(20));
v1.push_back(Data(30));
v1.push_back(Data(40));
v1.push_back(Data(50));
for_each(v1.begin(), v1.end(), mem_fun_ref(&Data::showData));
}
void test11()
{
vector v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
vector::iterator ret;
ret = find_if(v1.begin(), v1.end(), not1(bind2nd(greater(), 30))
);
if(ret != v1.end())
{
cout<<"找到的结果:"<<*ret<()) );
for_each(v1.begin(), v1.end(), [](int val){
cout<
void print01(int v)
{
cout << v << endl;
}
void test01()
{
vector nums;
nums.push_back(10);
nums.push_back(30);
nums.push_back(40);
nums.push_back(20);
nums.push_back(50);
for_each(nums.begin(),nums.end(),print01);
}
class Data{
public:
int count;
Data(){
count = 0;
}
void operator()(int v)
{
cout << v << " ";
count++;
}
};
void test02()
{
list nums;
nums.push_back(1);
nums.push_back(5);
nums.push_back(2);
nums.push_back(4);
nums.push_back(3);
cout << "数据:";
Data d = for_each(nums.begin(),nums.end(),Data());
cout << endl;
cout << "count:" << d.count << endl;
}
class Person{
private:
string name;
int age;
public:
Person(){}
Person(string name,int age):name(name),age(age){}
~Person(){}
void showInfo(){
cout << name << " " << age << endl;
}
};
void test03()
{
vector ps;
Person p1("张三",23);
Person p2("李四",29);
Person p3("王五",19);
ps.push_back(p1);
ps.push_back(p2);
ps.push_back(p3);
for_each(ps.begin(),ps.end(),mem_fun_ref(&Person::showInfo));
}
int setData(int a){
return a+100;
}
int setData2(int a,int x){
return a+x;
}
void test04()
{
vector vs;
srand(time(NULL));
for(int i = 0; i < 10; i++)
{
vs.push_back(rand() % 100);
}
for_each(vs.begin(),vs.end(),print01);
cout << endl;
list ls;
ls.resize(vs.size());
transform(vs.begin(),vs.end(),ls.begin(),setData);
for_each(ls.begin(),ls.end(),print01);
cout << endl;
list ls2;
ls2.resize(vs.size());
transform(vs.begin(),vs.end(),ls2.begin(),bind2nd(ptr_fun(setData2),10));
for_each(ls2.begin(),ls2.end(),print01);
cout << endl;
}
class Add{
public:
int operator()(int a,int b)
{
return a + b;
}
};
void test05()
{
vector vs01;
srand(time(NULL));
for(int i = 0; i < 10; i++)
{
vs01.push_back(rand() % 100);
}
for_each(vs01.begin(),vs01.end(),print01);
cout << endl;
vector vs02;
for(int i = 0; i < 10; i++)
{
vs02.push_back(rand() % 100);
}
for_each(vs02.begin(),vs02.end(),print01);
cout << endl;
vector vs03;
vs03.resize(vs01.size());
transform(vs01.begin(),vs01.end(),vs02.begin(),vs03.begin(),Add());
for_each(vs03.begin(),vs03.end(),print01);
cout << endl;
}
void test01()
{
vector vs;
vs.push_back(12);
vs.push_back(15);
vs.push_back(11);
vs.push_back(13);
vs.push_back(14);
vector::iterator it = find(vs.begin(),vs.end(),13);
if(it != vs.end())
{
cout << "找到了" << endl;
}
else{
cout << "不存在" << endl;
}
}
bool myIf(int x)
{
return x > 13;
}
void test02()
{
vector vs;
vs.push_back(12);
vs.push_back(15);
vs.push_back(11);
vs.push_back(13);
vs.push_back(14);
vector::iterator it = find_if(vs.begin(),vs.end(),myIf);
cout << *it << endl;
}
void test03()
{
vector vs;
vs.push_back(12);
vs.push_back(15);
vs.push_back(11);
vs.push_back(11);
vs.push_back(13);
vector::iterator it = adjacent_find(vs.begin(),vs.end());
cout << *it << endl;
}
/*
注意: 在无序序列中不可用
@param beg 容器开始迭代器
@param end 容器结束迭代器
@param value 查找的元素
@return bool 查找返回 true 否则 false
*/
bool binary_search(iterator beg, iterator end, value);
示例:
void test04()
{
vector vs;
vs.push_back(1);
vs.push_back(3);
vs.push_back(5);
vs.push_back(7);
vs.push_back(9);
bool b = binary_search(vs.begin(),vs.end(),7);
if(b){
cout << "存在" << endl;
}else{
cout << "不存在" << endl;
}
}
#include
void test05()
{
multiset s;
s.insert(1);
s.insert(2);
s.insert(2);
s.insert(3);
s.insert(5);
int num = count(s.begin(),s.end(),2);
cout << num << endl;
}
void test06()
{
multiset s;
s.insert(1);
s.insert(2);
s.insert(2);
s.insert(3);
s.insert(5);
//统计容器中大于2的数
int num = count_if(s.begin(),s.end(),bind2nd(greater(),2));
cout << num << endl;
//统计容器中小于2的数
int num02 = count_if(s.begin(),s.end(),bind2nd(less(),2));
cout << num02 << endl;
}
void test01()
{
vector v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
vector v2;
v2.push_back(2);
v2.push_back(4);
v2.push_back(6);
vector v3;
v3.resize(v1.size() + v2.size());
merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
for_each(v3.begin(),v3.end(),[](int x){
cout << x << " ";
});
cout << endl;
}
sort
void test02()
{
vector vs;
vs.push_back(10);
vs.push_back(13);
vs.push_back(9);
vs.push_back(4);
vs.push_back(18);
for_each(vs.begin(),vs.end(),[](int x){
cout << x << " ";
});
cout << endl;
//默认从小到大
sort(vs.begin(),vs.end());
for_each(vs.begin(),vs.end(),[](int x){
cout << x << " ";
});
cout << endl;
sort(vs.begin(),vs.end(),[](int x,int y){
return x > y;
});
for_each(vs.begin(),vs.end(),[](int x){
cout << x << " ";
});
cout << endl;
}
示例2:
class Person{
private:
string name;
int age;
public:
Person(){}
Person(string name,int age):name(name),age(age){}
~Person(){}
void showInfo(){
cout << name << " " << age << endl;
}
bool pSort(Person &p){
return this -> age < p.age;
}
};
void test03()
{
vector ps;
Person p1("张三",23);
Person p2("李四",29);
Person p3("王五",19);
ps.push_back(p1);
ps.push_back(p2);
ps.push_back(p3);
sort(ps.begin(),ps.end(),mem_fun_ref(&Person::pSort));
for_each(ps.begin(),ps.end(),mem_fun_ref(&Person::showInfo));
}
void test04()
{
vector vs;
vs.push_back(1);
vs.push_back(2);
vs.push_back(3);
vs.push_back(4);
vs.push_back(5);
random_shuffle(vs.begin(), vs.end());
for_each(vs.begin(),vs.end(),[](int x){
cout << x << " ";
});
cout << endl;
}
reverse
void test05()
{
vector vs;
vs.push_back(31);
vs.push_back(2);
vs.push_back(23);
vs.push_back(14);
vs.push_back(5);
sort(vs.begin(),vs.end());
for_each(vs.begin(),vs.end(),[](int x){
cout << x << " ";
});
cout << endl;
reverse(vs.begin(),vs.end());
for_each(vs.begin(),vs.end(),[](int x){
cout << x << " ";
});
cout << endl;
}
void test01()
{
vector vs;
vs.push_back(31);
vs.push_back(2);
vs.push_back(23);
vs.push_back(14);
vs.push_back(5);
vector newVs;
newVs.resize(vs.size());
copy(vs.begin(),vs.end(),newVs.begin());
for_each(newVs.begin(),newVs.end(),[](int x){
cout << x << endl;
});
}
void test02()
{
vector vs;
vs.push_back(31);
vs.push_back(2);
vs.push_back(23);
vs.push_back(14);
vs.push_back(5);
replace(vs.begin(),vs.end(),2,20);
for_each(vs.begin(),vs.end(),[](int x){
cout << x << endl;
});
}
void test03()
{
vector vs;
vs.push_back(31);
vs.push_back(2);
vs.push_back(23);
vs.push_back(14);
vs.push_back(5);
replace_if(vs.begin(),vs.end(),[](int x){
return x < 20;
},20);
for_each(vs.begin(),vs.end(),[](int x){
cout << x << endl;
});
}
void test04()
{
vector v01;
v01.push_back(1);
v01.push_back(2);
v01.push_back(3);
v01.push_back(4);
v01.push_back(5);
vector v02;
v02.push_back(6);
v02.push_back(7);
v02.push_back(8);
cout << "交换前:" << endl;
for_each(v01.begin(),v01.end(),[](int x){
cout << x << " ";
});
cout << endl;
for_each(v02.begin(),v02.end(),[](int x){
cout << x << " ";
});
cout << endl;
swap(v01,v02);
cout << "交换后:" << endl;
for_each(v01.begin(),v01.end(),[](int x){
cout << x << " ";
});
cout << endl;
for_each(v02.begin(),v02.end(),[](int x){
cout << x << " ";
});
cout << endl;
}
void test01()
{
vector v01;
v01.push_back(1);
v01.push_back(2);
v01.push_back(3);
v01.push_back(4);
v01.push_back(5);
int sum = 0;
sum = accumulate(v01.begin(),v01.end(),0);
cout << sum << endl;
}
void test02()
{
vector v01;
v01.push_back(1);
v01.push_back(2);
v01.push_back(3);
v01.push_back(4);
v01.push_back(5);
fill(v01.begin(),v01.end(),10);
for_each(v01.begin(),v01.end(),[](int x){
cout << x << " ";
});
cout << endl;
}
set_intersection
void test03()
{
vector v01;
v01.push_back(1);
v01.push_back(2);
v01.push_back(3);
v01.push_back(4);
v01.push_back(5);
vector v02;
v02.push_back(1);
v02.push_back(4);
v02.push_back(7);
vector v03;
//back_inserter:获取一个插入迭代器
set_intersection(v01.begin(),v01.end(),v02.begin(),v02.end(),back_inse
rter(v03));
for_each(v03.begin(),v03.end(),[](int x){
cout << x << " ";
});
cout << endl;
}
set_union
void test04()
{
vector v01;
v01.push_back(1);
v01.push_back(2);
v01.push_back(3);
v01.push_back(4);
v01.push_back(5);
vector v02;
v02.push_back(1);
v02.push_back(4);
v02.push_back(7);
vector v03;
//back_inserter:获取一个插入迭代器
set_union(v01.begin(),v01.end(),v02.begin(),v02.end(),back_inserter(v0
3));
for_each(v03.begin(),v03.end(),[](int x){
cout << x << " ";
});
cout << endl;
}
set_difference
void test05()
{
vector v01;
v01.push_back(1);
v01.push_back(3);
v01.push_back(5);
v01.push_back(7);
v01.push_back(9);
vector v02;
v02.push_back(9);
v02.push_back(11);
v02.push_back(13);
vector v03;
//back_inserter:获取一个插入迭代器
set_difference(v01.begin(),v01.end(),v02.begin(),v02.end(),back_insert
er(v03));
for_each(v03.begin(),v03.end(),[](int x){
cout << x << " ";
});
cout << endl;
}