博客
关于我
【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/

你可能感兴趣的文章
promise.all是并发执行吗_攻破面试灵魂拷问,解读Java并发编程的艺术,本文带你深入l理解...
查看>>
PyTorch-Tutorials【pytorch官方教程中英文详解】- 7 Optimization
查看>>
promise总结
查看>>
Propel项目改为基于TensorFlow.js
查看>>
PyTorch-Tutorials【pytorch官方教程中英文详解】- 6 Autograd
查看>>
properties出现中文乱码解决方法(万能)
查看>>
Property 'submit' of object #<HTMLFormElement> is not a function
查看>>
property--staticmethod--classmethod
查看>>
propertyGrid
查看>>
propertyPlaceholderConfigurer读取配置文件
查看>>
PyTorch-Tutorials【pytorch官方教程中英文详解】- 5 Build Model
查看>>
proteus三输入与非门名字_proteus 元件名称对照表
查看>>
Protobuf - 语法、字段使用规则、注意事项
查看>>
protobuf —— 快速上手
查看>>
protobuf —— 认识和安装
查看>>
Protobuf 三个关键字required、optional、repeated的理解
查看>>
ProtoBuf 原理详解
查看>>
Protobuf 实例(java)
查看>>
ProtoBuf 实际应用(java)
查看>>