概述
STL常用算法主要是由头文件《algorithm》、《functional》、《numeric》组成 《algorithm》是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历、赋值、修改等等 《numeric》体积较小,只包括几个在序列上面进行简单数学运算的模板函数 《functional》定义了一些模板类,用来声明函数对象(仿函数)
1. 常用遍历算法
算法原型:
for_each
transform
1.1 for_each
功能描述:
函数原型:
for_each(iterator beg, iterator end, _func);
示例:
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());
cout << endl;
}
1.2 transform
功能描述:
函数原型:
transform(iterator beg1, iterator end1, iterator beg2, _func);
示例:
class TransForm
{
public:
int operator()(int val)
{
return val;
}
};
class print
{
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> target;
target.resize(v.size());
transform(v.begin(), v.end(), target.begin(), TransForm());
for_each(target.begin(), target.end(), print());
cout << endl;
}
注意:搬运的目标容器必须提前开辟空间,否则无法正常搬运
2. 常用查找算法
算法原型:
find
find_if
adjacent_find
binary_search
count
count_if
2.1 find
功能描述:
- 查找指定元素,找到了返回元素的迭代器,找不到返回结束迭代器end()
函数原型:
ind(iterator beg, iterator end, value);
示例:
void test01()
{
vector<int> v;
for (int i = 0; i < 10; ++i)
{
v.push_back(i + 1);
}
vector<int>::iterator pos = find(v.begin(), v.end(), 6);
if (pos == v.end()) cout << "没有找到" << endl;
else cout << "找到了:" << *pos << endl;
}
class Person
{
public:
string name;
int age;
public:
Person(string name, int age)
{
this->name = name;
this->age = age;
}
bool operator==(const Person& p)
{
if (this->name == p.name && this->age == p.age) return true;
else return false;
}
};
void test02()
{
vector<Person> v;
Person p1("张三", 18);
Person p2("李四", 20);
Person p3("王五", 25);
Person p4("老六", 30);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector<Person>::iterator pos = find(v.begin(), v.end(), p2);
if (pos == v.end()) cout << "没有找到" << endl;
else cout << "找到了,姓名:" << pos->name << " 年龄:" << pos->age << endl;
}
注意:利用find查找容器中的指定元素,返回的是迭代器
2.2 find_if
功能描述:
函数原型:
find_if(iterator beg, iterator end, _Pred);
示例:
class GreaterFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
void test01()
{
vector<int> v;
for (int i = 0; i < 10; ++i)
{
v.push_back(i + 1);
}
vector<int>::iterator pos = find_if(v.begin(), v.end(), GreaterFive());
if (pos == v.end())
{
cout << "没有找到比5大的数字" << endl;
}
else
{
cout << "找到比5大的数字:" << *pos << endl;
}
}
class Person
{
public:
string name;
int age;
public:
Person(string name, int age)
{
this->name = name;
this->age = age;
}
};
class Greater20
{
public:
bool operator()(Person& p)
{
return p.age > 20;
}
};
void test02()
{
vector<Person> v;
Person p1("张三", 18);
Person p2("李四", 20);
Person p3("王五", 40);
Person p4("老六", 12);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector<Person>::iterator pos = find_if(v.begin(), v.end(), Greater20());
if (pos != v.end())
{
cout << "找到比20岁大的人了,name:" << pos->name << "age: " << pos->age << endl;
}
else
{
cout << "没有找到比20岁大的人了" << endl;
}
}
2.3 adjacent_find
功能:
函数原型:
adjacent_find(iterator beg, iterator end);
示例:
void test01()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(3);
v.push_back(4);
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
if (it != v.end())
{
cout << "找到相邻重复的元素了,是:" << *it << endl;
}
else
{
cout << "没有找到相邻重复的元素" << endl;
}
}
2.4 binary_search
功能描述:
函数原型:
bool binary_search(iterator beg, iterator end, value);
示例:
void test01()
{
vector<int> v;
for (int i = 0; i < 10; ++i) v.push_back(i + 1);
bool ret = binary_search(v.begin(), v.end(), 2);
if (ret) cout << "找到了" << endl;
else cout << "没有找到" << endl;
}
注意:二分查找的效率很高,但是要注意要查找的容器必须是有序的
2.5 count
功能描述:
函数原型:
count(iterator beg, iterator end, value);
示例:
void test01()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(2);
v.push_back(4);
v.push_back(2);
v.push_back(2);
int num = count(v.begin(), v.end(), 2);
cout << "2的个数为:" << num << endl;
}
class Person
{
public:
string name;
int age;
public:
Person(string name, int age)
{
this->name = name;
this->age = age;
}
bool operator==(const Person& p)
{
if (this->age == p.age) return true;
else return false;
}
};
void test02()
{
vector<Person> v;
Person p1("张三", 18);
Person p2("李四", 20);
Person p3("王五", 18);
Person p4("老六", 12);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
Person p("老八", 18);
int num = count(v.begin(), v.end(), p);
cout << "与p年龄相等的人数为:" << num << endl;
}
注意:统计自定义数据类型时,需要配合重载 operator==
2.6 count_if
功能描述:
函数原型:
count_if(iterator beg, iterator end, _Pred);
示例:
class Greater5
{
public:
bool operator()(int val)
{
return val > 5;
}
};
void test01()
{
vector<int> v;
for (int i = 0; i < 10; ++i) v.push_back(i + 1);
int num = count_if(v.begin(), v.end(), Greater5());
cout << "大于4的个数为:" << num << endl;
}
class Person
{
public:
string name;
int age;
public:
Person(string name, int age)
{
this->name = name;
this->age = age;
}
};
class AgeLess20
{
public:
bool operator()(const Person& p)
{
return p.age < 20;
}
};
void test02()
{
vector<Person> v;
Person p1("张三", 18);
Person p2("李四", 20);
Person p3("王五", 18);
Person p4("老六", 12);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
int num = count_if(v.begin(), v.end(), AgeLess20());
cout << "小于20岁的人数:" << num << endl;
}
3. 常用排序算法
算法原型:
sort
random_shuffle
merge
reverse
3.1 sort
功能描述:
函数原型:
sort(iterator beg, iterator end, _Pred);
示例:
void print(int val)
{
cout << val << " ";
}
void test01()
{
vector<int> v;
v.push_back(2);
v.push_back(4);
v.push_back(1);
v.push_back(3);
v.push_back(5);
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), print);
cout << endl;
}
3.2 random_shuffle
功能描述:
函数原型:
random_shuffle(iterator beg, iterator end);
示例:
void test01()
{
srand((unsigned int)time(NULL));
vector<int> v;
for (int i = 0; i < 10; ++i) v.push_back(i + 1);
for_each(v.begin(), v.end(), print);
cout << endl;
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
}
注意:random_shuffle洗牌算法比较使用,使用前记得加随机数种子srand()
3.3 merge
功能描述:
函数原型:
merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
示例:
void test01()
{
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 10; ++i)
{
v1.push_back(i);
v2.push_back(i + 1);
}
vector<int> target;
target.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), target.begin());
for_each(target.begin(), target.end(), print);
cout << endl;
}
注意:merge合并的两个容器必须是有序的
3.4 reverse
功能描述:
函数原型:
reverse(iterator beg, iterator end);
示例:
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
cout << "反转前:" << endl;
for_each(v.begin(), v.end(), print);
cout << endl;
cout << "反转后:" << endl;
reverse(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
}
|