一.遍历算法
1.for_each
//普通函数
void print01(int val) {
cout << val << "";
}
//仿函数
class print02 {
public:
void operator() (int val){
cout << val << " ";
}
};
void test01() {
vector<int>v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
for_each(v.begin(), v.end(), print01);
cout << endl;
for_each(v.begin(), v.end(), print02());
}
int main()
{
test01();
system("pause");
return 0;
}
2.transform
class Transform {
public:
int operator()(int v) {
return v;
}
};
class MyPrint {
public:
void operator()(int val) {
cout << val << " ";
}
};
void test01() {
vector<int>v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
vector<int>vTarget;//目标容器
vTarget.resize(v.size());//目标容器需要提前开辟空间
transform(v.begin(), v.end(), vTarget.begin(), Transform());
for_each(vTarget.begin(), vTarget.end(), MyPrint());
cout << endl;
}
二.查找算法
- find//查找元素
- find_if //按条件查找元素
- adjacent_find //查找相邻重复元素
- binary_search //二分查找
- count //统计元素个数
- count_if //按条件统计元素个数
1.find
find可以在容器中找指定的元素,它的返回值是迭代器
class Person {
public:
Person(string name,int age){
this->m_Name=name;
this->m_age = age;
}
//重载==
bool operator==(const Person &p) {
if (this->m_Name == p.m_Name && this->m_age == p.m_age) {
return true;
}
else {
return false;
}
}
string m_Name;
int m_age;
};
void test01() {
vector<int>v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
cout << endl;
//查找是否有5这个元素
vector<int>::iterator it= find(v.begin(), v.end(), 5);
if (it == v.end()) {
cout << "未找到" << endl;
}
else {
cout << "找到了" << endl;
}
}
void test02() {
vector<Person>v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector<Person>::iterator it = find(v.begin(), v.end(), p2);
if (it == v.end()) {
cout << "没有找到" << endl;
}
else {
cout << "找到了:姓名:" << it->m_Name << "年龄:" << it->m_age << endl;
}
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
2.find_if
class GreaterFive {
public:
bool operator()(int val) {
return val > 5;
}
};
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class Greater20 {
public:
bool operator()(Person &p)
{
return p.m_Age > 20;
}
};
void test01() {
vector<int>v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
cout << endl;
//查找是否有5这个元素
vector<int>::iterator it= find_if(v.begin(), v.end(), GreaterFive());
if (it == v.end()) {
cout << "未找到" << endl;
}
else {
cout << "找到了大于5的数:" << *it<<endl;
}
}
void test02() {
vector<Person>v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());
if (it == v.end()) {
cout << "没有找到" << endl;
}
else {
cout << "找到了:姓名:" << it->m_Name << "年龄:" << it->m_Age << endl;
}
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
3.adjacent_find:查找相邻重复元素
void test01() {
vector<int>v;
v.push_back(0);
v.push_back(2);
v.push_back(0);
v.push_back(3);
v.push_back(1);
v.push_back(4);
v.push_back(3);
v.push_back(3);
vector<int>::iterator it= adjacent_find(v.begin(), v.end());
if (it == v.end()) {
cout << "未找到" << endl;
}
else {
cout << "找到了相邻重复元素:" << *it<<endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
4.binary_search
查找指定元素是否存在,注意:必须是有序序列,否则查找结果未知。
void test01() {
vector<int>v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
bool ret = binary_search(v.begin(), v.end(), 9);
if (ret) {
cout << "找到了" << endl;
}
else {
cout << "没找到" << endl;
}
}
5.count
统计元素个数
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
bool operator==(const Person &p) {
if (this->m_Age == p.m_Age) {
return true;
}
else {
return false;
}
}
string m_Name;
int m_Age;
};
void test01() {
vector<int>v;
v.push_back(10);
v.push_back(40);
v.push_back(30);
v.push_back(40);
v.push_back(20);
v.push_back(40);
int num = count(v.begin(), v.end(), 40);
cout << "40的元素个数为:" << num << endl;
}
void test02() {
vector<Person>v;
Person p1("aaa", 30);
Person p2("bbb", 30);
Person p3("ccc", 30);
Person p4("ddd", 40);
Person p("eee", 30);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
int num=count(v.begin(), v.end(), p);
cout << "和eee同岁的人员个数:" << num << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
6.count_if
按条件统计元素个数
class GreaterFive {
public:
bool operator()(int val) {
return val > 5;
}
};
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class Greater20 {
public:
bool operator()(Person &p)
{
return p.m_Age > 20;
}
};
void test01() {
vector<int>v;
v.push_back(0);
v.push_back(3);
v.push_back(4);
v.push_back(5);
v.push_back(6);
v.push_back(7);
v.push_back(8);
cout << endl;
//查找是否有5这个元素
int num = count_if(v.begin(), v.end(), GreaterFive());
cout << "找到了大于5的数:" << num<<"个" <<endl;
}
void test02() {
vector<Person>v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
int num = count_if(v.begin(), v.end(), Greater20());
cout << "找到了年龄大于20的人"<<num<<"个"<< endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}