for_each
#pragma once
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
void printfs(int val)
{
cout << val << endl;
}
void test01()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
for_each(v.begin(), v.end(), printfs);
}
int main()
{
test01();
return 0;
}
transform
#pragma once
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class Printfs
{
public:
void operator()(int val)
{
cout << val << "\t";
}
};
class TransForm
{
public:
int operator()(int val)
{
return val;
}
};
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int> v1;
v1.resize(v.size());
transform(v.begin(), v.end(), v1.begin(),TransForm());
for_each(v1.begin(), v1.end(), Printfs());
}
int main()
{
test01();
return 0;
}
find
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>::iterator it = find(v.begin(),v.end(),5);
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到:\t" << *it << endl;
}
}
class Person
{
public:
Person(string name, int age) :Name(name), Age(age) {}
string getName()
{
return Name;
}
int getAge()
{
return Age;
}
bool operator==(const Person &p)
{
if (this->Name == p.Name && this->Age == p.Age)
{
return true;
}
else
{
return false;
}
}
private:
string Name;
int Age;
};
void test02()
{
vector<Person> v;
Person p1("a", 1);
Person p2("b", 2);
Person p3("c", 3);
Person p4("d", 4);
Person p5("e", 5);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
vector<Person>::iterator it = find(v.begin(),v.end(),p2);
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到:\t" << it->getName() << "\t" << it->getAge() << endl;
}
}
int main()
{
test02();
return 0;
}
find_if
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
class Person
{
public:
bool operator()(int val)
{
return val > 5;
}
};
class Person_tow
{
public:
Person_tow(string name, int age) :Name(name), Age(age) {}
string getName()
{
return Name;
}
int getAge()
{
return Age;
}
private:
string Name;
int Age;
};
class Ppp
{
public:
bool operator()(Person_tow &p)
{
return p.getAge() > 2;
}
};
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>::iterator it = find_if(v.begin(), v.end(), Person());
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到:\t" << *it << endl;
}
}
void test02()
{
vector<Person_tow> v;
Person_tow p1("aaa",1);
Person_tow p2("bbb",2);
Person_tow p3("ccc",3);
Person_tow p4("ddd",4);
Person_tow p5("eee",5);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
vector<Person_tow>::iterator it = find_if(v.begin(),v.end(),Ppp());
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到:\t" << it->getName() << "\t" << it->getAge() << endl;
}
}
int main()
{
test02();
return 0;
}