STL中常用算法可大致分为四类:
非变异算法:不直接修改所操作的容器内容的算法。
变异算法:可以修改容器内容的算法。
排序算法:包括对序列进行排序和合并的算法、搜索算法以及有序序列上的集合操作。
数值算法:对容器内容进行数值计算。
1.查找算法
包含在头文件中,用来提供元素排序策略。
注意:_InIt _First和_FwdIt _First分别代表InputIterator和ForwardIterator,表示输入迭代器和前向迭代器。
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main(int argc, char* argv[])
{
int iarr[] = { 0, 1, 2, 3, 4, 5, 6, 6, 6, 7, 8 };
vector<int> iv(iarr, iarr + sizeof(iarr) / sizeof(int));
/*** adjacent_find: 在iterator对标识元素范围内,查找一对相邻重复元素 ***/
cout << "adjacent_find: ";
cout << *adjacent_find(iv.begin(), iv.end()) << endl;
/*** count: 利用等于操作符,把标志范围内的元素与输入值比较,返回相等元素个数。 ***/
// 原型: count(_InIt _First, _InIt _Last, const _Ty& _Val)
cout << "count(==7): ";
cout << count(iv.begin(), iv.end(), 6) << endl;// 统计6的个数
/*** count_if: 利用输入的操作符,对标志范围内的元素进行操作,返回结果为true的个数。 ***/
// 原型: count_if(_InIt _First, _InIt _Last, _Pr _Pred)
// 统计小于7的元素的个数 :9个
cout << "count_if(<7): ";
cout << count_if(iv.begin(), iv.end(), bind2nd(less<int>(), 7)) << endl;
/*** binary_search: 在有序序列中查找value,找到返回true。 ***/
// 原型: bool binary_search(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
cout << "binary_search: ";
cout << binary_search(iv.begin(), iv.end(), 4) << endl; // 找到返回true
/*** equal_range: 功能类似equal,返回一对iterator,第一个表示lower_bound,第二个表示upper_bound。 ***/
// 原型: equal_range(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
pair<vector<int>::iterator, vector<int>::iterator> pairIte;
pairIte = equal_range(iv.begin(), iv.end(), 3);
cout << "pairIte.first:" << *(pairIte.first) << endl;// lowerbound 3
cout << "pairIte.second:" << *(pairIte.second) << endl; // upperbound 4
/*** find: 利用底层元素的等于操作符,对指定范围内的元素与输入值进行比较。 ***/
// 原型: _InIt find(_InIt _First, _InIt _Last, const _Ty& _Val)
cout << "find: ";
cout << *find(iv.begin(), iv.end(), 4) << endl; // 返回元素为4的元素的下标位置
/*** find_if: 使用输入的函数代替等于操作符执行find。 ***/
// 原型: _InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred)
cout << "find_if: " << *find_if(iv.begin(), iv.end(), bind2nd(greater<int>(), 2)) << endl; // 返回大于2的第一个元素的位置:3
/*** search: 给出两个范围,返回一个ForwardIterator,查找成功指向第一个范围内第一次出现子序列的位置。 ***/
// 原型: _FwdIt1 search(_FwdIt1 _First1, _FwdIt1 _Last1, _FwdIt2 _First2, _FwdIt2 _Last2)
// 在iv中查找 子序列 2 3 第一次出现的位置的元素
int iarr3[3] = { 2, 3 };
vector<int> iv3(iarr3, iarr3 + 2);
cout << "search: " << *search(iv.begin(), iv.end(), iv3.begin(), iv3.end()) << endl;
/*** search_n: 在指定范围内查找val出现n次的子序列。 ***/
// 原型: _FwdIt1 search_n(_FwdIt1 _First1, _FwdIt1 _Last1, _Diff2 _Count, const _Ty& _Val)
// 在iv中查找 2个6 出现的第一个位置的元素
cout << "search_n: " << *search_n(iv.begin(), iv.end(), 2, 6) << endl;
return 0;
}
2.排序算法
包含在头文件中,用来判断容器中是否包含某个值。
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional> // 定义了greater<int>()
using namespace std;
// 要注意的技巧
template <class T>
struct display
{
void operator()(const T& x) const
{
cout << x << " ";
}
};
// 如果想从大到小排序,可以采用先排序后反转的方式,也可以采用下面方法:
// 自定义从大到小的比较器,用来改变排序方式
bool Comp(const int& a, const int& b) {
return a > b;
}
int main(int argc, char* argv[])
{
int iarr1[] = { 0, 1, 2, 3, 4, 5, 6, 6, 6, 7, 8 };
vector<int> iv1(iarr1, iarr1 + sizeof(iarr1) / sizeof(int));
vector<int> iv2(iarr1 + 4, iarr1 + 8); // 4 5 6 6
vector<int> iv3(15);
/*** merge: 合并两个有序序列,存放到另一个序列 ***/
// iv1和iv2合并到iv3中(合并后会自动排序)
merge(iv1.begin(), iv1.end(), iv2.begin(), iv2.end(), iv3.begin());
cout << "merge合并后: ";
for_each(iv3.begin(), iv3.end(), display<int>());
cout << endl;
/*** random_shuffle: 对指定范围内的元素随机调整次序。 ***/
int iarr2[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
vector<int> iv4(iarr2, iarr2 + sizeof(iarr2) / sizeof(int));
// 打乱顺序
random_shuffle(iv4.begin(), iv4.end());
cout << "random_shuffle打乱后: ";
for_each(iv4.begin(), iv4.end(), display<int>());
cout << endl;
/*** nth_element: 将范围内的序列重新排序。 ***/
// 将小于iv.begin+5的放到左边
nth_element(iv4.begin(), iv4.begin() + 5, iv4.end());
cout << "nth_element重新排序后: ";
for_each(iv4.begin(), iv4.end(), display<int>());
cout << endl;
/*** reverse: 将指定范围内元素重新反序排序。 ***/
reverse(iv4.begin(), iv4.begin());
cout << "reverse翻转后: ";
for_each(iv4.begin(), iv4.end(), display<int>());
cout << endl;
/*** sort: 以升序重新排列指定范围内的元素。 ***/
// sort(iv4.begin(), iv4.end(), Comp); // 也可以使用自定义Comp()函数
sort(iv4.begin(), iv4.end(), greater<int>());
cout << "sort排序(倒序): ";
for_each(iv4.begin(), iv4.end(), display<int>());
cout << endl;
/*** stable_sort: 与sort类似,不过保留相等元素之间的顺序关系。 ***/
int iarr3[] = { 0, 1, 2, 3, 3, 4, 4, 5, 6 };
vector<int> iv5(iarr3, iarr3 + sizeof(iarr3) / sizeof(int));
stable_sort(iv5.begin(), iv5.end(), greater<int>());
cout << "stable_sort排序(倒序): ";
for_each(iv5.begin(), iv5.end(), display<int>());
cout << endl;
return 0;
}
3.删除和替换算法
包含在头文件中,实现删除和替换功能。
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional> // 定义了greater<int>()
using namespace std;
template <class T>
struct display
{
void operator()(const T&x) const
{
cout << x << " ";
}
};
int main(int argc, char* argv[])
{
int iarr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
vector<int> iv1(iarr1, iarr1 + sizeof(iarr1) / sizeof(int));
vector<int> iv2(9);
/*** copy: 复制序列 ***/
// 原型: _OutIt copy(_InIt _First, _InIt _Last,_OutIt _Dest)
copy(iv1.begin(), iv1.end(), iv2.begin());
cout << "copy(iv2): ";
for_each(iv2.begin(), iv2.end(), display<int>());
cout << endl;
/*** copy_backward: 与copy相同,不过元素是以相反顺序被拷贝。 ***/
// 原型: _BidIt2 copy_backward(_BidIt1 _First, _BidIt1 _Last,_BidIt2 _Dest)
copy_backward(iv1.begin(), iv1.end(), iv2.rend());
cout << "copy_backward(iv2): ";
for_each(iv2.begin(), iv2.end(), display<int>());
cout << endl;
/*** remove: 删除指定范围内所有等于指定元素的元素。 ***/
// 原型: _FwdIt remove(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
remove(iv1.begin(), iv1.end(), 5); // 删除元素5
cout << "remove(iv1): ";
for_each(iv1.begin(), iv1.end(), display<int>());
cout << endl;
/*** remove_copy: 将所有不匹配元素复制到一个制定容器,返回OutputIterator指向被拷贝的末元素的下一个位置。 ***/
// 原型: _OutIt remove_copy(_InIt _First, _InIt _Last,_OutIt _Dest, const _Ty& _Val)
vector<int> iv3(8);
remove_copy(iv1.begin(), iv1.end(), iv3.begin(), 4); // 去除4 然后将一个容器的元素复制到另一个容器
cout << "remove_copy(iv3): ";
for_each(iv3.begin(), iv3.end(), display<int>());
cout << endl;
/*** remove_if: 删除指定范围内输入操作结果为true的所有元素。 ***/
// 原型: _FwdIt remove_if(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
remove_if(iv3.begin(), iv3.end(), bind2nd(less<int>(), 6)); // 将小于6的元素 "删除"
cout << "remove_if(iv3): ";
for_each(iv3.begin(), iv3.end(), display<int>());
cout << endl;
/*** remove_copy_if: 将所有不匹配元素拷贝到一个指定容器。 ***/
// 原型: _OutIt remove_copy_if(_InIt _First, _InIt _Last,_OutIt _Dest, _Pr _Pred)
// 将iv1中小于6的元素 "删除"后,剩下的元素再复制给iv3
remove_copy_if(iv1.begin(), iv1.end(), iv2.begin(), bind2nd(less<int>(), 4));
cout << "remove_if(iv2): ";
for_each(iv2.begin(), iv2.end(), display<int>());
cout << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional> // 定义了greater<int>()
using namespace std;
template <class T>
struct display
{
void operator()(const T&x) const
{
cout << x << " ";
}
};
int main(int argc, char* argv[])
{
int iarr[] = { 8, 10, 7, 8, 6, 6, 7, 8, 6, 7, 8 };
vector<int> iv(iarr, iarr + sizeof(iarr) / sizeof(int));
/*** replace: 将指定范围内所有等于vold的元素都用vnew代替。 ***/
// 原型: void replace(_FwdIt _First, _FwdIt _Last, const _Ty& _Oldval, const _Ty& _Newval)
// 将容器中6 替换为 3
replace(iv.begin(), iv.end(), 6, 3);
cout << "replace(iv): ";
for_each(iv.begin(), iv.end(), display<int>()); // 由于_X是static 所以接着 增长
cout << endl; // iv:8 10 7 8 3 3 7 8 3 7 8
/*** replace_copy: 与replace类似,不过将结果写入另一个容器。 ***/
// 原型: _OutIt replace_copy(_InIt _First, _InIt _Last, _OutIt _Dest, const _Ty& _Oldval, const _Ty& _Newval)
vector<int> iv2(12);
// 将容器中3 替换为 5,并将结果写入另一个容器。
replace_copy(iv.begin(), iv.end(), iv2.begin(), 3, 5);
cout << "replace_copy(iv2): ";
for_each(iv2.begin(), iv2.end(), display<int>());
cout << endl; // iv2:8 10 7 8 5 5 7 8 5 7 8 0(最后y一个残留元素)
/*** replace_if: 将指定范围内所有操作结果为true的元素用新值代替。 ***/
// 原型: void replace_if(_FwdIt _First, _FwdIt _Last, _Pr _Pred, const _Ty& _Val)
// 将容器中小于 5 替换为 2
replace_if(iv.begin(), iv.end(), bind2nd(less<int>(), 5), 2);
cout << "replace_copy(iv): ";
for_each(iv.begin(), iv.end(), display<int>());
cout << endl; // iv:8 10 7 8 2 5 7 8 2 7 8
/*** replace_copy_if: 与replace_if,不过将结果写入另一个容器。 ***/
// 原型: _OutIt replace_copy_if(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pred, const _Ty& _Val)
// 将容器中小于 5 替换为 2,并将结果写入另一个容器。
replace_copy_if(iv.begin(), iv.end(), iv2.begin(), bind2nd(equal_to<int>(), 8), 9);
cout << "replace_copy_if(iv2): ";
for_each(iv2.begin(), iv2.end(), display<int>());
cout << endl; // iv2:9 10 7 8 2 5 7 9 2 7 8 0(最后一个残留元素)
int iarr3[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, };
vector<int> iv3(iarr3, iarr3 + sizeof(iarr3) / sizeof(int));
int iarr4[] = { 8, 10, 7, 8, 6, 6, 7, 8, 6, };
vector<int> iv4(iarr4, iarr4 + sizeof(iarr4) / sizeof(int));
/*** swap: 交换存储在两个对象中的值。 ***/
// 原型: _OutIt replace_copy_if(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pred, const _Ty& _Val)
// 将两个容器中的第一个元素交换
swap(*iv3.begin(), *iv4.begin());
cout << "swap(iv3): ";
for_each(iv3.begin(), iv3.end(), display<int>());
cout << endl;
/*** swap_range: 将指定范围内的元素与另一个序列元素值进行交换。 ***/
// 原型: _FwdIt2 swap_ranges(_FwdIt1 _First1, _FwdIt1 _Last1, _FwdIt2 _Dest)
// 将两个容器中的全部元素进行交换
swap_ranges(iv4.begin(), iv4.end(), iv3.begin());
cout << "swap_range(iv3): ";
for_each(iv3.begin(), iv3.end(), display<int>());
cout << endl;
/*** unique: 清除序列中相邻的重复元素,和remove类似,它也不能真正删除元素。 ***/
// 原型: _FwdIt unique(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
unique(iv3.begin(), iv3.end());
cout << "unique(iv3): ";
for_each(iv3.begin(), iv3.end(), display<int>());
cout << endl;
/*** unique_copy: 与unique类似,不过把结果输出到另一个容器。 ***/
// 原型: _OutIt unique_copy(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pred)
unique_copy(iv3.begin(), iv3.end(), iv4.begin());
cout << "unique_copy(iv4): ";
for_each(iv4.begin(), iv4.end(), display<int>());
cout << endl;
return 0;
}
4.排列组合算法
包含在头文件中,用来提供计算给定集合按一定顺序的所有可能排列组合。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <class T>
struct display
{
void operator()(const T&x) const
{
cout << x << " ";
}
};
int main(int argc, char* argv[])
{
int iarr[] = { 12, 17, 20, 22, 23, 30, 33, 40 };
vector<int> iv(iarr, iarr + sizeof(iarr) / sizeof(int));
/*** next_permutation: 取出当前范围内的排列,并重新排序为下一个字典序排列。***/
// 原型: bool next_permutation(_BidIt _First, _BidIt _Last)
// 生成下一个排列组合(字典序)
next_permutation(iv.begin(), iv.end());
for_each(iv.begin(), iv.end(), display<int>());
cout << endl;
/*** prev_permutation: 取出指定范围内的序列并将它重新排序为上一个字典序排列。 ***/
// 原型: bool prev_permutation(_BidIt _First, _BidIt _Last)
prev_permutation(iv.begin(), iv.end());
for_each(iv.begin(), iv.end(), display<int>());
cout << endl;
return 0;
}