网站死链修复,网页特效大全,海阳有没有做企业网站的,深圳华汇设计STL常用算法#xff08;1#xff09; 前言简介一.遍历算法1.for_each2.transform 二.查找算法1.find2.find_if3.adjacent_find4.binary_search5.count6.cout_if 三.排序算法1.sort2.random_shuffle3.merge4.reverse 总结 前言 stl系列主要讲述有关stl的文章#xff0c;使用S… STL常用算法1 前言简介一.遍历算法1.for_each2.transform 二.查找算法1.find2.find_if3.adjacent_find4.binary_search5.count6.cout_if 三.排序算法1.sort2.random_shuffle3.merge4.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
#includevector
using namespace std;void print(int num) {//打印函数cout num ;
}int main() {vectorint 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() {vectorint n1 { 1, 2, 3, 4, 5 };vectorint 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() {vectorint n { 1, 2, 3, 4, 5};vectorint::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() {vectorint n { 1, 2, 3, 4, 5};vectorint::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() {vectorint n { 1 ,2, 4, 2, 5 };vectorint::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() {vectorint 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() {vectorint 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() {vectorint 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() {vectorint 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() {vectorint 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() {vectorint n1 { 1,3,5,7 };vectorint n2 { 2,4,6,8 };vectorint 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() {vectorint n { 1,2,3,4,5 };reverse(n.begin(), n.end());for (int i : n) {cout i ;}return 0;
}总结 希望大家点赞收藏我会尽快更新STL