加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C++ search_n(STL search_n)搜索算法详解

发布时间:2020-12-16 07:39:59 所属栏目:百科 来源:网络整理
导读:search_n() 算法会搜索给定元素的匹配项,它在序列中连续出现了给定的次数。它的前两个参数是定义搜索范围的正向迭代器,第 3 个参数是想要查找的第 4 个元素的连续匹配次数。例如: std::vectordouble values {2.7,2.7,3.14,2.7};double value {3.14};int ti
search_n() 算法会搜索给定元素的匹配项,它在序列中连续出现了给定的次数。它的前两个参数是定义搜索范围的正向迭代器,第 3 个参数是想要查找的第 4 个元素的连续匹配次数。例如:
std::vector<double> values {2.7,2.7,3.14,2.7};
double value {3.14};
int times {3};
auto iter = std::search_n(std::begin(values),std::end(values),times,value);
if (iter != std::end(values))
    std::cout << times << " successive instances of " << value<< " found starting index " << std::distance (std::begin (values),iter) <<std::endl;
这段代码会在 values 容器中查找第一个有 3 个 value 实例的匹配项。它找到的序列的索引为 3。注意用来指定个数的第三个参数必须是无符号整型;如果不是,编译这段代码会产生警告。

这里用 == 来比较元素,但也可以提供额外的断言参数来代替它。当然,这不需要定义一个判断是否相等的比较。下面是一个完整的示例:
// Searching using search_n() to find freezing months
#include <iostream>                              // For standard streams
#include <vector>                                // For vector container
#include <algorithm>                             // For search_n()
#include <string>                                // For string class
using std::string;

int main()
{
    std::vector<int> temperatures {65,75,56,48,31,28,32,29,40,41,44,50};
    int max_temp {32};
    int times {3};
    auto iter = std::search_n(std::begin(temperatures),std::end(temperatures),max_temp,[](double v,double max){return v <= max; });
    std::vector<string> months {"January","February","March","April","May","June","July","August","September","October","November","December"};
    if(iter != std::end(temperatures))
        std::cout << "It was " << max_temp << " degrees or below for " << times<< " months starting in " << months[std::distance(std::begin(temperatures),iter)]<< std::endl;
}
temperatures 容器中保存了一年中每个月份的平均温度。search_n() 的最后一个参数是一个 lambda 表达式的谓词,当元素小于等于 max_temp 时,它会返回 true。month 容器中保存月份的名称。

表达式 std::distance(std::begin(temperatures),iter) 会返回 temperatures 这个序列的第一个元素的索引,这个序列可以使谓词返回 true。这个值用来索引选择 months 中月份的名称,所以这段代码会输出如下内容:

It was 32 degrees or below for 3 months starting in May

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读