【STL】7.STL常用算法(1)
STL常用算法(1)
- 前言
- 简介
- 一.遍历算法
- 1.for_each
- 2.transform
- 二.查找算法
- 1.find
- 2.find_if
- 3.adjacent_find
- 4.binary_search
- 5.count
- 6.cout_if
- 三.排序算法
- 1.sort
- 2.random_shuffle
- 3.merge
- 4.reverse
- 总结
前言
stl系列主要讲述有关stl的文章,使用STL可以大大提高程序开发的效率和代码的可维护性,且在算法比赛中,STL可以帮助我们更方便地实现各种算法。提高我们的效率。
简介
算法主要是头文件algorithm,functional,numeric组成
1.algorithm是所有STL头文件中最大的一个,范围涉及到比较,交换,查找,遍历操作,复制,修改等等
2.numeric体积很小,只包括几个在序列上面进行简单的数学运算的模板函数
3.functional定义了一些模板类,用以声明函数对象
一.遍历算法
都在algorithm头文件中
for_each();//遍历容器
transform();//搬运容器到另一个容器中
1.for_each
for_each(first, last, f);//first和last为输出迭代器定义了要操作的范围,为左闭右开即[first, last)。f是一个函数或函数对象
如:
#include <iostream>
#include <algorithm>
#include<vector>
using namespace std;
void print(int num) {//打印函数
cout << num << " ";
}
int main() {
vector<int> n = { 1, 2, 3, 4, 5 };
for_each(n.begin(), n.end(), print);
return 0;
}
2.transform
taransform(first1, last1, first2, f)//first1和last1为输出迭代器定义了要操作的范围,为左闭右开即[first, last)。firts2是目标容器的迭代器,f是一个函数或函数对象
如:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int re(int num) {//函数可以返回任意值如num*num等
return num;
}
void print(int num) {
cout << num << " ";
}
int main() {
vector<int> n1 = { 1, 2, 3, 4, 5 };
vector<int> n2;
n2.resize(n1.size());//要提前开辟空间
transform(n1.begin(), n1.end(), n2.begin(), re);
for_each(n2.begin(), n2.end(), print);
return 0;
}
二.查找算法
find();//查找元素
find_if();//按条件查找元素
adjacent_find();//查找相邻重复元素
binary_search();//二分查找法
count();//统计元素个数
count_if();//按条件统计元素个数
1.find
find(first, last, value);//find的查找范围为[first, last),value是要查找的值,r如果没有就返回last,有就返回所在位置
如:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> n = { 1, 2, 3, 4, 5};
vector<int>::iterator i = find(n.begin(), n.end(), 1);
if (i == n.end()) {
cout << "no" << endl;
}
else {
cout << "yes" << endl;
}
return 0;
}
2.find_if
find_if(first, last, f);//在指定范围内查找满足特定条件的第一个元素,f为函数
如:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool f(int num) {
return num > 3;
}
int main() {
vector<int> n = { 1, 2, 3, 4, 5};
vector<int>::iterator i = find_if(n.begin(), n.end(), f);
if (i != n.end()) {
cout << *i << endl;
}
else {
cout << "no" << endl;
}
return 0;
}
3.adjacent_find
find_if(first, end);//查找相邻重复元素,返回相邻元素的第一个位置的迭代器
如:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> n = { 1 ,2, 4, 2, 5 };
vector<int>::iterator p = adjacent_find(n.begin(), n.end());
if (p != n.end()) {
cout << *p << endl;
}
else {
cout << "no" << endl;
}
return 0;
}
4.binary_search
binary_search();//查找指定元素是否存在,放回一个布尔值
//在无序条件序列不可用
如:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> n = { 1, 2, 3, 4, 5 };
bool b = binary_search(n.begin(), n.end(), 3);
if (b) {
cout << "yes" << endl;
}
else {
cout << "no" << endl;
}
return 0;
}
5.count
count(first, last, value);//查找一定范围内是否有值为value
如:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> n = { 1,2,3,4,5,6 };
bool b = count(n.begin(), n.end(), 4);
if (b) {
cout << "yes" << endl;
}
else {
cout << "no" << endl;
}
return 0;
}
6.cout_if
cout_if(first, last, f);//查找一定范围内,查找f功能的函数,有几个
如:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool f(int num) {
return num > 3;
}
int main() {
vector<int> n = { 1,2,3,4,5 };
int sum = count_if(n.begin(), n.end(), f);
cout << sum << endl;
return 0;
}
三.排序算法
sort();//对容器内元素进行排序
random_shuffle();//指定范围内的元素随机调整次序
merge();//合并容器元素,两个容器必须是有序的
reverse();反转
1.sort
sort(first, last, comp);//在first到last的范围内从小到大排序,可以利用comp自定义
如:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> n = { 3,4,2,1,7,6 };
sort(n.begin(), n.end());
for (int i : n) {
cout << i << " ";
}
return 0;
}
2.random_shuffle
rand_suffle(first, last);//指定范围内的元素随机调整次序
如:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> n = { 1,2,3,4,5,6 };
srand((unsigned int)time(0));
random_shuffle(n.begin(), n.end());
for (int i : n) {
cout << i << " ";
}
return 0;
}
3.merge
merge(first1 end1, first2, end2, first3);//将first1到last1和first2到last2合并到first3中,其中两个容器必须是有序的
如:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> n1 = { 1,3,5,7 };
vector<int> n2 = { 2,4,6,8 };
vector<int> n3(8);
merge(n1.begin(), n1.end(), n2.begin(), n2.end(), n3.begin());
for (int i : n3) {
cout << i << " ";
}
return 0;
}
4.reverse
reverse(first, last);//在first和last范围内反转
如:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> n = { 1,2,3,4,5 };
reverse(n.begin(), n.end());
for (int i : n) {
cout << i << " ";
}
return 0;
}
总结
希望大家点赞收藏我会尽快更新STL!!!