博客
关于我
【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 append() 与列表上的 + 运算符,为什么这些会给出不同的结果?
查看>>
Python APP自动化测试工具adb与Monkey使用详解
查看>>
Python APP自动化测试框架Appium详解
查看>>
Python APP自动化测试框架开发实战
查看>>
python argparse模块
查看>>
Python asyncio库的学习和使用
查看>>
Python AttributeError:“dict“对象没有属性“append“
查看>>
Python base64和hashlib模块
查看>>
python basic programs
查看>>
python bert_gen.py 报错Unable to load weights from pytorch checkpoint file for......
查看>>
python binascii.Error: Incorrect padding
查看>>
Python bool() 函数能否为无效参数引发异常?
查看>>
Python C 程序子进程在“for line in iter“处挂起
查看>>
Python Celery:自动化测试平台定时任务必备的三方库
查看>>
python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令
查看>>
Python CONNECT 4 CHECK WIN函数
查看>>
python cos,Python cos(90)和cos(270)不是0
查看>>
python进阶(4):Python 脚本文件重启自身进程
查看>>
python ctypes库中动态链接库加载方式
查看>>
python cv2 图像( np array )转 HObject
查看>>