STL——查找算法及实例

news/2025/3/26 18:29:58/

一 前言

        STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成。要使用 STL中的算法函数必须包含头文件<algorithm>,对于数值算法须包含<numeric>,<functional>中则定义了一些模板类,用来声明函数对象。
STL中算法大致分为四类:
1)、非可变序列算法:指不直接修改其所操作的容器内容的算法。
2)、可变序列算法:指可以修改它们所操作的容器内容的算法。
3)、排序算法:包括对序列进行排序和合并的算法、搜索算法以及有序序列上的集合操作。
4)、数值算法:对容器内容进行数值计算。

 二 查找算法(13个)

判断容器中是否包含某个值

    adjacent_find

  在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的ForwardIterator。否则返回last。重载版本使用输入的二元操作符代替相等的判断。

#include <iostream>
#include <algorithm>
#include <vector>int main() {std::vector<int> numbers = {1, 2, 3, 4, 4, 5, 6};// Find the first pair of adjacent repeated elementsauto it = std::adjacent_find(numbers.begin(), numbers.end());if (it != numbers.end()) {std::cout << "Found adjacent repeated elements: " << *it << " and " << *(it + 1) << std::endl;} else {std::cout << "No adjacent repeated elements found" << std::endl;}return 0;
}输出:Found adjacent repeated elements: 4 and 4


   binary_search

        在有序序列中查找value,找到返回true。重载的版本实用指定的比较函数对象或函数指针来判断相等。

#include <iostream>
#include <algorithm>
#include <vector>int main() {std::vector<int> numbers = {1, 2, 3, 4, 5, 6};int value = 3;// Check if value exists in the sorted sequencebool found = std::binary_search(numbers.begin(), numbers.end(), value);if (found) {std::cout << "Value " << value << " found" << std::endl;} else {std::cout << "Value " << value << " not found" << std::endl;}return 0;
}输出
Value 3 found


    count 

        利用等于操作符,把标志范围内的元素与输入值比较,返回相等元素个数。

#include <iostream>
#include <algorithm>
#include <vector>int main() {std::vector<int> numbers = {1, 2, 3, 2, 2, 4, 5};int value = 2;// Count the occurrences of value in the rangeint count = std::count(numbers.begin(), numbers.end(), value);std::cout << "Number of occurrences of " << value << ": " << count << std::endl;return 0;
}输出:
Number of occurrences of 2: 3


    count_if 

        利用输入的操作符,对标志范围内的元素进行操作,返回结果为true的个数。

#include <iostream>
#include <algorithm>
#include <vector>bool isEven(int number) {return number % 2 == 0;
}int main() {std::vector<int> numbers = {1, 2, 3, 4, 5, 6};// Count the number of even elements in the rangeint count = std::count_if(numbers.begin(), numbers.end(), isEven);std::cout << "Number of even elements: " << count << std::endl;return 0;
}输出:
Number of even elements: 3


    equal_range

        功能类似equal,返回一对iterator,第一个表示lower_bound,第二个表示upper_bound。

std::vector<int> nums = {1, 2, 3, 4, 4, 5};
auto range = std::equal_range(nums.begin(), nums.end(), 4);
// range.first指向第一个等于4的元素位置,range.second指向第一个大于4的元素位置
// 在这个例子中,range.first指向索引3处的元素(值为4),range.second指向索引5处的元素(值为5)


    find  

        利用底层元素的等于操作符,对指定范围内的元素与输入值进行比较。当匹配时,结束搜索,返回该元素的一个InputIterator。

std::vector<int> nums = {1, 2, 3, 4, 5};
auto it = std::find(nums.begin(), nums.end(), 3);
// it指向索引2处的元素(值为3)


    find_end

        在指定范围内查找"由输入的另外一对iterator标志的第二个序列"的最后一次出现。找到则返回最后一对的第一个ForwardIterator,否则返回输入的"另外一对"的第一个ForwardIterator。重载版本使用用户输入的操作符代替等于操作。

std::vector<int> nums = {1, 2, 3, 4, 1, 2, 3, 4};
std::vector<int> subseq = {3, 4};
auto it = std::find_end(nums.begin(), nums.end(), subseq.begin(), subseq.end());
// it指向索引6处的元素(值为3),即最后一次出现子序列{3, 4}的位置

   find_first_of        

        在指定范围内查找"由输入的另外一对iterator标志的第二个序列"中任意一个元素的第一次出现。重载版本中使用了用户自定义操作符。

std::vector<int> nums = {1, 2, 3, 4, 5};
std::vector<int> search_nums = {4, 6};
auto it = std::find_first_of(nums.begin(), nums.end(), search_nums.begin(), search_nums.end());
// it指向索引3处的元素(值为4),即首次出现search_nums中任意一个元素{4, 6}的位置


    find_if  

        使用输入的函数代替等于操作符执行find。

std::vector<int> nums = {1, 2, 3, 4, 5};
auto it = std::find_if(nums.begin(), nums.end(), [](int num) { return num % 2 == 0; });
// it指向索引1处的元素(值为2),即第一个满足条件(num % 2 == 0)的元素的位置


    lower_bound

         返回一个ForwardIterator,指向在有序序列范围内的可以插入指定值而不破坏容器顺序的第一个位置。重载函数使用自定义比较操作。

std::vector<int> nums = {1, 2, 3, 4, 5};
auto it = std::lower_bound(nums.begin(), nums.end(), 4);
// it指向索引3处的元素(值为4),即第一个大于等于4的元素的位置


    upper_bound

        返回一个ForwardIterator,指向在有序序列范围内插入value而不破坏容器顺序的最后一个位置,该位置标志一个大于value的值。重载函数使用自定义比较操作。

std::vector<int> nums = {1, 2, 3, 4, 5};
auto it = std::upper_bound(nums.begin(), nums.end(), 4);
// it指向索引4处的元素(值为5),即第一个大于4的元素的位置


    search:

         给出两个范围,返回一个ForwardIterator,查找成功指向第一个范围内第一次出现子序列(第二个范围)的位置,查找失败指向last1。重载版本使用自定义的比较操作。

#include <iostream>
#include <algorithm>
#include <vector>bool customCompare(int a, int b) {// 自定义比较函数,检查是否a和b相差为1return (std::abs(a - b) == 1);
}int main() {std::vector<int> vec1 {1, 2, 3, 4, 5};std::vector<int> vec2 {3, 4};auto it = std::search(vec1.begin(), vec1.end(), vec2.begin(), vec2.end());if (it != vec1.end()) {std::cout << "子序列在位置 " << std::distance(vec1.begin(), it) << " 处找到" << std::endl;} else {std::cout << "未找到子序列" << std::endl;}// 使用自定义比较函数it = std::search(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), customCompare);if (it != vec1.end()) {std::cout << "子序列在位置 " << std::distance(vec1.begin(), it) << " 处找到" << std::endl;} else {std::cout << "未找到子序列" << std::endl;}return 0;
}


    search_n

        在指定范围内查找val出现n次的子序列。重载版本使用自定义的比较操作。

#include <iostream>
#include <algorithm>
#include <vector>bool customCompare(int a, int b) {// 自定义比较函数,检查是否a和b相差为1return (std::abs(a - b) == 1);
}int main() {std::vector<int> numbers {1, 2, 3, 4, 5, 6, 7, 8, 9};// 在numbers中查找连续出现3个值为2的子序列auto it = std::search_n(numbers.begin(), numbers.end(), 3, 2);if (it != numbers.end()) {std::cout << "Found the subsequence at index: "<< std::distance(numbers.begin(), it) << std::endl;} else {std::cout << "Subsequence not found!" << std::endl;}// 使用自定义比较函数在numbers中查找连续出现3个相邻的元素it = std::search_n(numbers.begin(), numbers.end(), 3, 0, customCompare);if (it != numbers.end()) {std::cout << "Found the subsequence at index: "<< std::distance(numbers.begin(), it) << std::endl;} else {std::cout << "Subsequence not found!" << std::endl;}return 0;
}


http://www.ppmy.cn/news/1150635.html

相关文章

springboot 配置 servlet filter 2

springboot 配置 servlet filter 2 以配置Druid为例 Servlet WebServlet(urlPatterns "/druid/*",initParams {WebInitParam(name "loginUsername", value "admin"),// 登录用户名WebInitParam(name "loginPassword", value …

unity URP 利用particle system制作简单的shader交互

首先这里制作了一个简单交互&#xff0c;使用shader grapgh&#xff0c;根据计算距离和变化数值的差实现交互后的扩散&#xff0c;同时计算消散遮罩让它逐渐谈去。 将他赋予材质物体&#xff0c;根据脚本传入位置和逐渐变化的大小后&#xff0c;呈现这样的效果。 但是&#xff…

Windows提权方法论

Windows提权方法论 1.溢出漏洞提权2.计划任务提权3.SAM文件提权4.启动项提权5.不带引号的服务路径提权 1.溢出漏洞提权 溢出提权攻击的基本原理是&#xff0c;通过向目标系统发送过长的输入数据&#xff0c;超出了程序所分配的缓冲区大小&#xff0c;导致溢出。攻击者可以利用…

小程序使用uni.createAnimation只执行一次的问题

思路&#xff1a; 在页面创建的时候&#xff0c;创建一个临时动画对象调用 step() 来表示一组动画完成通过动画实例的export方法导出动画数据传递给组件的animation属性还原动画页面卸载的时候&#xff0c;清除动画数据 <template><view class"content"&g…

【mysql】 bash: mysql: command not found

在linux 服务器上安装了mysql 也可以正常运行。 但是执行命令&#xff0c;系统提示&#xff1a;bash: mysql: command not found bash:mysql:找不到命令 执行的命令是&#xff1a; mysql -u root -h 127.0.0.1 -p由于系统默认会查找的 /usr/bin/ 中下的命令&#xff0c;如…

机器人制作开源方案 | 扫地机器人

1. 功能描述 扫地机器人是现代家庭清洁的得力助手&#xff0c;能够自主规划清扫路径&#xff0c;避开障碍物&#xff0c;有效覆盖整个清洁区域。扫地机器人的出现极大地减轻了家庭清洁的负担&#xff0c;节省了时间和精力&#xff0c;它可以定期清理地面&#xff0c;确保家居环…

lvgl overview

https://docs.lvgl.io/master/get-started/quick-overview.html 图像元素 屏幕上的图像元素以单根对象树管理&#xff0c;默认情况下根对象就是显示器的活动显示器——act_scr&#xff0c;在显示器注册时创建,参见lv_disp_drv_register。 应用也可以自己创建screen的根对象&…

机器学习与模式识别作业----决策树属性划分计算

文章目录 1.决策树划分原理1.1.特征选择1--信息增益1.2.特征选择2--信息增益比1.3.特征选择3--基尼系数 2.决策树属性划分计算题2.1.信息增益计算2.2.1.属性1的信息增益计算2.2.2.属性2的信息增益计算2.2.3.属性信息增益比较 2.2.信息增益比计算2.3.基尼系数计算 1.决策树划分原…