博客
关于我
【C++系列】C++中的常用遍历算法
阅读量:247 次
发布时间:2019-03-01

本文共 695 字,大约阅读时间需要 2 分钟。

常用遍历算法

学习目标

掌握常用的遍历算法

算法简介

在编程中,遍历容器是一项常见操作,常用的算法包括for_eachtransform

5.1.1 for_each

功能描述

for_each算法用于遍历容器中的元素,执行指定的函数或函数对象。

函数原型

for_each(iterator beg, iterator end, _func);
  • beg:开始迭代器
  • end:结束迭代器
  • _func:函数或函数对象

示例

#include 
#include
void print01(const std::vector
& vec) { for (int i : vec) { std::cout << i << " "; } std::cout << std::endl;}int main() { std::vector
my_vector = {1, 2, 3, 4, 5}; std::for_each(my_vector.begin(), my_vector.end(), print01); return 0;}

在上述代码中,std::for_each遍历了my_vector中的所有元素,并调用了print01函数进行处理。

在本节中,我们重点学习了for_each算法的使用方法,这是C++标准库中最常用的遍历容器的算法之一。在接下来的内容中,我们将详细分析transform算法的功能及其应用场景。

转载地址:http://msxv.baihongyu.com/

你可能感兴趣的文章
Python 中生成器与普通函数的区别
查看>>
Python 中的 *tuple 和 **dict 是什么意思?
查看>>
Python 中的 filter() 函数:筛选可迭代对象元素
查看>>
Python 中的 Pillow 不允许我打开图像(“超出限制“)
查看>>
Python 中的 threading 模块和 multiprocessing 模块有何区别?
查看>>
Python 中的 threading 模块和 multiprocessing 模块有何区别?
查看>>
Python 中的 Turtle 库详解
查看>>
Python 中的 __slots__ 属性有什么作用?
查看>>
Python 中的... 三点、箭头(->)、/ 分别的作用
查看>>
python读取json数据的id_python处理JSON数据
查看>>
Python 中的Duck Typing
查看>>
Python 中的for in 遍历生成字典、添加判断条件if
查看>>
Python 中的Lock与RLock
查看>>
Python 中的range(),arange()函数
查看>>
Python 中的内存管理机制
查看>>
Python 中的函数包装器:模型运行时和调试
查看>>
Python 中的列表理解和 lambda
查看>>
Python 中的多层for in嵌套循环使用
查看>>
Python 中的多线程(01/4)
查看>>
Python 中的多进程(01/2):简介
查看>>