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

你可能感兴趣的文章
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>