一、STL简介?
1、为了建立数据结构与算法的一套标准,诞生了STL,STL即标准模板库,STL广义划分:容器(container),算法(algorithm),迭代器(iterator),STL几乎所有的代码都采用了模板类或模板函数
2、SLT六大组件:
容器:各种数据结构
算法:提供大量函数(排序、查找、计数、操作......)
迭代器:用于提供一种方法顺序访问一个聚合对象中的各个元素,而不需要暴露该对象的内部表示形式
仿函数:行为类似函数,可作为算法的某种策略
适配器:将一个类的接口转换为另一个类的接口,使原本因接口不兼容而不能一起运行的类一起“工作”
空间配置器:空间的配置和管理
二、常用的泛型算法
1.不修改序列操作
①find:查找满足特定判别标准的首个元素
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int v1 = 3;
int v2 = 8;
vector<int> v_num{2,5,3,7,4,1};
auto ret1 = find(begin(v_num),end(v_num),v1);
auto ret2 = find(begin(v_num),end(v_num),v2);
if(ret1 != end(v_num))
cout << "v_num contains " << v1 << endl;
else
cout << "v_num does not contain " << v1 << endl;
if(ret2 != end(v_num))
cout << "v_num contains " << v2 << endl;
else
cout << "v_num does not contain " << v2 << endl;
return 0;
}
2.修改序列操作
①copy:将某一范围的元素复制到另一位置
#include <iostream>
#include <vecotr>
#include <algorithm>
using namespace std;
int main()
{
vector<int> from_vector{0,1,2,3,4,5,6,7,8,9};
vector<int> to_vector(10);
copy(from_vector.begin(),from_vector.end(),to_vector.begin());
int i = 0;
int n = to_vector.size();
while(n--)
cout << to_vector.at(i++) << " " ;
cout << endl;
return 0;
}
②fill:将某一范围的元素赋值为同一个值
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
//将v中的每一个元素都赋值为-2
fill(v.begin(), v.end(), -2);
//取出v中的每一个元素输出
for (auto elem : v) {
cout << elem << " ";
}
cout << endl;
return 0;
}
③swap:交换两个元素的值
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 3;
// 交换前
cout << a << " " << b << endl;
swap(a,b);
// 交换后
cout << a << " " << b << endl;
return 0;
}
④reverse:倒置某范围的元素
#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v{1,2,3,4,5,6,7,8,9};
reverse(begin(v),end(v));
for(auto elem : v)
cout << elem << " ";
cout << endl;
int a[10] = {0,1,2,3,4,5,6,7,8,9};
reverse(begin(a),end(a));
for(auto elem : a)
cout << elem << " ";
cout << endl;
return 0;
}
⑤unique:删除范围内连续重复的元素
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cctype>
using namespace std;
int main()
{
// 移除重复元素
vector<int> v{1,2,3,1,2,3,3,4,5,4,5,6,7};
sort(v.begin(), v.end()); // 1 1 2 2 3 3 3 4 4 5 5 6 7
auto last = unique(v.begin(), v.end());
// v 现在保有 {1 2 3 4 5 6 7 x x x x x x} ,其中 x 不确定
v.erase(last, v.end()); //消除x x x范围的空间元素,并缩小物理空间
for (auto i : v)
cout << i << " ";
cout << endl;
return 0;
}
3.排序操作
①sort、is_sorted:将元素升序排列并判断元素是否已升序排列
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v{1,5,3,1,78,3,2,4,7};
sort(v.begin(),v.end());
for(auto elem : v)
cout << elem << " ";
cout << endl;
bool f = is_sorted(v.begin(),v.end());
if(f)
cout << "v 升序排列" << endl;
else
cout << "v 没有按照升序排列" << endl;
return 0;
}
4.二分搜索操作(在已排序基础上)
①binary_search:查找某元素是否在某范围内
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int arr[] = {2,5,4,3,78,9,54,1,254,6};
sort(begin(arr),end(arr));
bool f = binary_search(begin(arr),end(arr),5);
if(f)
cout << "yes" << endl;
else
cout << "no" << endl;
return 0;
}
②merge:归并两个已排序的范围
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v1{2,4,1,65,3,5,7};
vector<int> v2{23,43,65,1,32,6};
vector<int> v3(13);
sort(v1.begin(),v1.end());
sort(v2.begin(),v2.end());
//归并
merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
for(int elem : v3)
cout << elem << " ";
cout << endl;
return 0;
}
5.最大最小操作
①max_element:查找范围内最大元素,还有min_element,minmax_element同理
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v{23,54,-1,-2,4,7,65,4,3};
auto ret = max_element(v.begin(),v.end());
cout << "max in " << distance(v.begin(),ret) << endl;
return 0;
}
6.比较操作
①equal:比较两个范围元素是否相等
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
vector<char> v1{'a','c','e','w','q'};
vector<char> v2{'a','c','e','w','q'};
vector<char> v3{'a','c','e'};
vector<int> v4{97,99,101};
bool f = equal(v1.begin(),v1.end(),v2.begin(),v2.end());
if(f)
cout << "v1 = v2" << endl;
else
cout << "v1 != v2" << endl;
f = equal(v1.begin(),v1.end(),v3.begin(),v3.end());
if(f)
cout << "v1 = v3" << endl;
else
cout << "v1 != v3" << endl;
f = equal(v3.begin(),v3.end(),v4.begin(),v4.end());
if(f)
cout << "v3 = v4" << endl;
else
cout << "v3 != v4" << endl;
return 0;
}
结果显示:v1 = v2
v1 != v3
v3 = v4
总结
以上就是一些比较常用的泛型算法,可以尝试更换数据类型加以实验和巩固,还有很多泛型算法这里都没有提到,可以自行到网上搜索资料学习