C++ search_n(STL search_n)搜索算法详解
C++ 详解 STL search 搜索算法
2023-06-13 09:11:55 时间
search_n() 算法会搜索给定元素的匹配项,它在序列中连续出现了给定的次数。它的前两个参数是定义搜索范围的正向迭代器,第 3 个参数是想要查找的第 4 个元素的连续匹配次数。例如:
std::vector double values {2.7, 2.7, 2.7, 3.14, 3.14, 3.14, 2.7, 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), times, 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
21788.html
gohtml相关文章
- C++中set用法详解
- c++语言截取字符串,详解C++ string常用截取字符串方法
- C++ STL 详解
- C++stl库_c++库
- C++中STL-set详解
- c++的链表-C++实现简单链表
- C/C++中连接函数strcat的应用(简单讲解)详解编程语言
- C++箴言:避免构造或析构函数中调用虚函数详解编程语言
- Web QQ 协议 登录加密算法 —— VC++实现详解编程语言
- C++ vector,STL vector(可变长的动态数组)详解
- C++ deque,STL deque(双向队列详解)
- C++11移动构造函数详解
- C++常对象和常成员函数详解
- C++ multiset,STL multiset详解
- C++ deque使用、创建及初始化详解
- C++ queue(STL queue)用法详解
- C++ map(STL map)删除元素(erase函数删除元素)详解
- C++ hash(STL hash)及其函数模板用法详解
- C++ set迭代器(STL set迭代器)详解
- C++ partial_sort(STL partial_sort)排序算法详解
- C++ is_sorted(STL is_sorted)排序算法详解
- C++ find_if(STL find_if)查找算法详解
- C++ find_end(STL find_end)算法详解
- C++ upper_bound(STL upper_bound)二分查找算法详解
- C++输入流迭代器(STL输入流迭代器)详解
- C++输出流迭代器(STL输出流迭代器)详解
- C++ partial_sum(STL partial_sum)部分和计算方法详解
- C++类对象的创建和使用(详解版)
- C++ share_ptr智能指针使用详解
- C++类模板用法详解
- C++ list,STL list(双向链表)详解
- C++ STL move_iterator移动迭代器用法详解
- 解析c++中参数对象与局部对象的析构顺序的详解
- 深入C++函数映射的使用详解