常用算法:
·常用算法主要由头文件<algorthm> <functional> <numeric>组成
·<algorithm>是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历操作、复制修改等等
·<numeric>体积很小,只包括几个序列上面进行简单数学运算的模板函数
·<functional>定义了一些模板类,用以声明函数对象
常用遍历算法:
for_each(iterator beg, iterator end, _func); //遍历容器
transform(iterator beg1, iterator end1, iterator beg2, _func); //搬运容器到另一个容器
常用的查找算法:
find((iterator beg, iterator end, val) //查找元素
find_if (iterator beg, iterator end, _Pred) //按条件查找
abjacent_find (iterator beg, iterator end) //查找相邻重复元素
bool binary_search (iterator beg, iterator end, value) //二分查找法
//查找指定元素,查到返回true 否则返回false. 在无序序列中不可用
count(iterator beg, iterator end, value) //统计元素个数
count_if (iterator beg, iterator end, _Pred) //按条件统计元素个数
常用的排序算法:
sort (iterator beg, iterator end, _Pred); //按照_Pred规则排序,默认升序
random_shuffle (iterator beg, iterator end); //指定范围内的元素,随机调整次序
merge (iterator beg1, iterator end1, iterator beg2, iterator end2, iterator vTarget.beg);
//将两个指定范围内的容器元素合并到目标容器中
//两个容器必须是有序的,且顺序一致
//需要提前分配目标容器的内存
reverse (iterator beg, iterator end); //将指定范围内的元素反转
常用的拷贝和替换算法
copy (iterator beg1, iterator end1, iterator beg2);
//将指定容器范围内的元素复制到另一容器中
//需要提前开辟接收容器的内存
replace (iterator beg, iterator end, oldvalue, newvalue);
//将容器中指定范围内的旧元素替换为新元素
replace_if (iterator beg, iterator end, _Pred, newvalue);
//将指定范围内满足条件的元素,替换为newvalue
swap (container c1, container c2);
//互换两个容器的元素 同类型容器
常用的算术生成算法
#include <numeric>
accumulate (iterator beg, iterator end, value); //累加操作
//value 起始累加值
fill (iterator beg, iterator end, value);
//向指定范围的容器中填充指定元素
常用的集合算法
//以下算法需要提前开辟目标容器的空间
//两个集合必须是有序序列
set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator vTarget.beg);
//交集
//返回目标容器的最后一个元素的迭代器地址
set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator vTarget.beg);
//并集
set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator vTarget.beg);
//求范围1中元素对范围2中元素的差集
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
//内建函数对象头文件
#include <functional>
#include <numeric>
//打印函数
void myPrint(int val)
{
cout << val << endl;
}
//打印仿函数
class MyPrint
{
public:
void operator()(int val) {
cout << val << endl;
}
};
//返回原值函数
class Transform
{
public:
int operator()(int val) {
return val;
}
};
//自定义从大到小排序函数
bool Mycompare(int a, int b)
{
return a > b;
}
//自定义小于3的返回真仿函数
class Lower3
{
public:
bool operator()(int val) {
return val < 3;
}
};
for_each和transform
void test1()
{
//for_each
vector<int> v;
v.push_back(10);
v.push_back(30);
v.push_back(50);
v.push_back(20);
for_each(v.begin(), v.end(), myPrint);
cout << "************************************" << endl;
for_each(v.begin(), v.end(), MyPrint());
//transform
vector<int> v2;
v2.push_back(100);
v2.push_back(500);
//目标容器要提前开辟空间
v2.resize(v.size() + v2.size());
//会覆盖掉,目标容器起始位置之后的内容
transform(v.begin(), v.end(), v2.begin() + 2, Transform());
cout << "*********************************" << endl;
for_each(v.begin(), v.end(), MyPrint());
cout << "*********************************" << endl;
for_each(v2.begin(), v2.end(), myPrint);
}
class Person
{
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
//重载== 底层find知道如何对比person数据类型
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;
};
find\find_if\binary_search\count
void test2()
{
vector<int> v;
//find
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
//查找容器中是否有5这个元素
vector<int> :: iterator it = find(v.begin(), v.end(), 5);
vector<int> :: iterator it = find_if(v.begin(), v.end(), GreaterFive());
//注意容器必须是有序序列
bool ret = binary_search(v.begin(), v.end(), 9);
int num = count(v.begin(), v.end(), 3);
cout << "3的个数为: " << num << endl;
int num2 = count_if(v.begin(), v.end(), GreaterFive());
if (it == v.end()) {
cout << "没有找到" << endl;
}
else {
cout << *it << endl;
}
//自定义数据类型
vector<Person> v2;
Person p1("zhangsan", 20);
Person p2("lisi", 23);
Person p3("wangwu", 10);
Person p4("liuer", 18);
v2.push_back(p1);
v2.push_back(p2);
v2.push_back(p3);
v2.push_back(p4);
//需要重载 ==
vector<Person> ::iterator it2 = find(v2.begin(), v2.end(), p2);
vector<Person> ::iterator it3 = find_if(v2.begin(), v2.end(), Greater20());
int num = count (v.begin(), v.end(), p2);
int num3 = count_if(v2.begin(), v2.end(), Greater20());
cout << "大于20岁的人数为: " << num3 << endl;
if (it2 == v2.end()) {
cout << "没有找到" << endl;
}
else {
cout << "存在" << endl;
}
if (it3 == v2.end()) {
cout << "没找到" << endl;
}
else {
cout << "姓名为: " << it3->m_Name << " 年龄为 : " << it3->m_Age << endl;
}
}
adjacent_find
void test3()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(1);
v.push_back(3);
v.push_back(4);
v.push_back(3);
v.push_back(3);
vector<int> ::iterator pos = adjacent_find(v.begin(), v.end());
if (pos == v.end()) {
cout << "没有找到" << endl;
}
else {
cout << "存在相邻重复元素为: " << *pos << endl;
}
}
void test4()
{
vector<int> v;
vector<int> v2;
for (int i = 0; i < 10; i++) {
v.push_back(i);
v2.push_back(i - 1);
}
for_each(v.begin(), v.end(), Myprint());
cout << endl;
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), Myprint());
cout << endl;
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), Myprint());
cout << endl;
sort(v.begin(), v.end(), Mycompare);
for_each(v.begin(), v.end(), Myprint());
cout << endl;
//开辟空间
vector<int> v3;
v3.resize(v.size() + v2.size());
//v和v2顺序不一致导致崩了,调整一下v1排序
sort(v.begin(), v.end());
merge(v.begin(), v.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), v3.end(), Myprint());
cout << endl;
reverse(v.begin(), v.end());
for_each(v.begin(), v.end(), Myprint());
cout << endl;
//copy,merge需要开辟足够空间
v2.resize(v2.size() + v.size());
copy(v.begin(), v.end(), v2.begin() + 3);
for_each(v2.begin(), v2.end(), Myprint());
cout << endl;
replace(v2.begin(), v2.end(), 5, 888888);
for_each(v2.begin(), v2.end(), Myprint());
cout << endl;
replace_if(v2.begin(), v2.end(), Lower3(), 9999);
for_each(v2.begin(), v2.end(), Myprint());
cout << endl;
swap(v, v2);
for_each(v2.begin(), v2.end(), Myprint());
cout << endl;
int sum = accumulate(v2.begin(), v2.end(), 1000);
cout << sum << endl;
fill(v.begin() + 3, v.end(), 6);
for_each(v.begin(), v.end(), Myprint());
cout << endl;
vector<int> v4;
//开辟适合的空间
v4.resize(min(v.size(), v2.size()));
//必须是有序的
sort(v.begin(), v.end());
sort(v2.begin(), v2.end());
set_intersection(v.begin(), v.end(), v2.begin(), v2.end(), v4.begin());
for_each(v4.begin(), v4.end(), Myprint());
cout << endl;
v4.resize(v.size() + v2.size());
set_union(v.begin(), v.end(), v2.begin(), v2.end(), v4.begin());
for_each(v4.begin(), v4.end(), Myprint());
cout << endl;
set_difference(v.begin(), v.end(), v2.begin(), v2.end(), v4.begin());
for_each(v4.begin(), v4.end(), Myprint());
cout << endl;
}
int main()
{
test1();
test2();
test3();
test4();
system("pause");
return 0;
}
黑马程序员课程学习笔记