文章目录
STL常用算法【遍历、查找和统计】
1、for_each
- 实现遍历容器
练习代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
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 + 1);
}
//普通函数
for_each(v.begin(), v.end(), print01);
cout << endl;
//仿函数
for_each(v.begin(), v.end(), print02());
}
int main()
{
test01();
return 0;
}
2、transform
- 搬运容器到另一个容器中
练习代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class Transform
{
public:
int operator()(int val)
{
return val*10;
}
};
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 + 1);
}
vector<int> vtarget;//目标容器
vtarget.resize(v.size());//目标容器需要提前开辟空间
transform(v.begin(), v.end(), vtarget.begin(), Transform());
for_each(vtarget.begin(), vtarget.end(), MyPrint());
cout << endl;
}
int main()
{
test01();
return 0;
}
3、find
- 查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器
end()
练习代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
using namespace std;
//查找内置数据类型
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i + 1);
}
//查找容器中是否有5这个元素
vector<int>::iterator it = find(v.begin(), v.end(), 5);
if (it == v.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到了: " << *it << endl;
}
}
//查找自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_age = age;
this->m_name = name;
}
//重载 == 让底层知道如何比较自定义数据类型
bool operator==(const Person& p)
{
if (this->m_age == p.m_age && this->m_name == p.m_name)
{
return true;
}
else
{
return false;
}
}
string m_name;
int m_age;
};
void test02()
{
vector<Person> v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
Person pp("bbb", 20);
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(), pp);
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到了!" << endl;
cout << "naem:" << it->m_name << " " << "age:" << it->m_age << " ";
}
cout << endl;
}
int main()
{
test01();
test02();
return 0;
}
4、find_if
- 按条件查找元素
练习代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
using namespace std;
//内置数据类型练习
class GreaterSix
{
public:
bool operator()(int val)
{
return val > 6;
}
};
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i + 1);
}
//查找有没有大于6的数
vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterSix());
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到了大于6的数字为:" << *it << endl;
}
}
//自定义数据类型练习
class Person
{
public:
Person(string name, int age)
{
this->m_age = age;
this->m_name = name;
}
//重载 == 让底层知道如何比较自定义数据类型
string m_name;
int m_age;
};
class Greater20
{
public:
bool operator()(Person& p)
{
return p.m_age > 20;
}
};
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);
//找年龄大于20的人
vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到了" << endl;
cout << "name:" << it->m_name << " " << "age:" << it->m_age << endl;
}
}
int main()
{
test01();
test02();
return 0;
}
5、adjacent_find
- 查找相邻重复元素
练习代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
using namespace std;
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i + 1);
}
v.push_back(10);
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到了:" << *it << endl;
}
}
int main()
{
test01();
return 0;
}
6、binary_search
- 查找指定元素是否存在
- 在无序序列中不可用
练习代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
using namespace std;
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i + 1);
}
//看看5这个元素是不是存在
//需要注意的是该容器必须是有序序列
//如果是无序序列,结果未知
bool ret = binary_search(v.begin(), v.end(), 5);
if (!ret)
{
cout << "没有找到" << endl;
}
else
{
cout << "找到了" <<endl;
}
}
int main()
{
test01();
return 0;
}
7、count
- 统计元素个数
练习代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
using namespace std;
//内置数据类型
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i + 1);
}
v.push_back(10);
int cnt = count(v.begin(), v.end(), 10);
cout << "10的元素个数为:" << cnt << endl;
}
//自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
//operator==
bool operator==(const Person& p)
{
if (this->m_age == p.m_age)
{
return true;
}
else
{
return false;
}
}
string m_name;
int m_age;
};
void test02()
{
vector<Person> v;
Person p1("aaa", 12);
Person p2("bbb", 32);
Person p3("ccc", 12);
Person p4("ddd", 52);
Person p("aaa", 12);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
//和p同岁数的人有多少个
int cnt = count(v.begin(), v.end(), p);
cout <<"和p同岁的人有:"<< cnt << endl;
}
int main()
{
test01();
test02();
return 0;
}
8、count_if
- 按条件统计元素个数
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
using namespace std;
//内置数据类型
class Greater20
{
public:
bool operator()(int val)
{
return val > 20;
}
};
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i + 1);
}
v.push_back(30);
v.push_back(50);
int cnt = count_if(v.begin(), v.end(), Greater20());
cout << "大于20的元素个数为:" << cnt << endl;
}
//自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
class AgeGreater20
{
public:
bool operator()(const Person& p)
{
return p.m_age > 20;
}
};
void test02()
{
vector<Person> v;
Person p1("aaa", 12);
Person p2("bbb", 32);
Person p3("ccc", 12);
Person p4("ddd", 52);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
//大于20岁的人有多少个
int cnt = count_if(v.begin(), v.end(), AgeGreater20());
cout <<"大于20岁的人有:"<< cnt << endl;
}
int main()
{
test01();
test02();
return 0;
}
🤣🤣🤣
排序、拷贝和替换算法见下篇笔记