当前位置: 首页 > news >正文

【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!!!

http://www.lryc.cn/news/543325.html

相关文章:

  • uniapp 本地数据库多端适配实例(根据运行环境自动选择适配器)
  • 百度觉醒,李彦宏渴望光荣
  • 【算法工程】大模型局限性新发现之解决能连github但无法clone项目的问题
  • SOME/IP-SD -- 协议英文原文讲解3
  • 软件测试八股文,软件测试常见面试合集【附答案】
  • 数据结构秘籍(一)线性数据结构
  • TFChat:腾讯大模型知识引擎(DeepSeek R1)+飞书机器人实现AI智能助手
  • 使用消息队列怎样防止消息重复?
  • MySQL安装多版本与版本切换
  • Docker02 - 深入理解Docker
  • 检查SSH安全配置-sshd服务端未认证连接最大并发量配置
  • HarmonyOS Design 介绍
  • C++中的多重继承
  • Java基础第14天-坦克大战【1】
  • Java线程池入门04
  • 【论文笔记-ECCV 2024】AnyControl:使用文本到图像生成的多功能控件创建您的艺术作品
  • 计算机毕业设计 ——jspssm519Springboot 的幼儿园管理系统
  • 山东大学软件学院人工智能导论实验之知识库推理
  • 【Uniapp-Vue3】点击将内容复制到剪切板
  • 英伟达 Isaac Sim仿真平台体验【2】
  • 低代码与开发框架的一些整合[3]
  • deepseek-r1-centos-本地服务器配置方法
  • C语言实现通讯录项目
  • Effective Java读书笔记 draft
  • DeepSeek 助力 Vue 开发:打造丝滑的滑块(Slider)
  • wordpress使用CorePress主题设置项总结
  • 逆向pyinstaller打包的exe软件,获取python源码(6)
  • 蓝桥杯 五子棋对弈
  • 【单片机】MSP430MSP432入门
  • 货车一键启动无钥匙进入手机远程启动的正确使用方法