假设有这样的一个vector:

vector<int> line={1,2,3,4,5,6,7,8,9};

需要输出vector里的每个元素,主函数如下:

void showvec(const vector<int>& line)
{
} int main(int argc, char** argv) {
vector<int> line = {1, 2, 3, 4, 5, 6, 7, 8, 9};
showvec(line);
return 0;
}

方法一:for循环迭代器输出

void showvec(const vector<int>& line) {
for (vector<int>::const_iterator iter = line.cbegin();iter != line.cend(); iter++) {
cout << (*iter) << endl;
}
}

或者使用c++新增的语义auto

void showvec(const vector<int>& line) {
for (auto iter = line.cbegin(); iter != line.cend(); iter++) {
cout << (*iter) << endl;
}
}

方法二:for_each加函数

template <typename T>
void printer(const T& line) {
cout << line;
cout << endl;
} void showvec(const vector<int>& line) {
for_each(line.cbegin(), line.cend(), printer<int>);
}

方法三:for区间遍历

for (auto lin : line) {
cout << lin;
}

  

C++11中vector的几种遍历方法的更多相关文章

  1. 论C++11 中vector的N种遍历方法

    随着C++11标准的出现,C++标准添加了许多有用的特性,C++代码的写法也有比较多的变化. vector是经常要使用到的std组件,对于vector的遍历,本文罗列了若干种写法. (注:本文中代码为 ...

  2. Jquery中each的三种遍历方法

    Jquery中each的三种遍历方法 $.post("urladdr", { "data" : "data" }, function(dat ...

  3. Java中Map的三种遍历方法

    Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历.   告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...

  4. jquery中each的3种遍历方法

    1.选择器+遍历 $('div').each(function (i){ i就是索引值 this 表示获取遍历每一个dom对象 }); 2.选择器+遍历 $('div').each(function  ...

  5. Jquery中each的3种遍历方式

    学习目标: 参考博文: https://blog.csdn.net/honey_th/article/details/7404273 一.Jquery中each的几种遍历方法 1. 选择器+遍历 &l ...

  6. 谈谈vector容器的三种遍历方法

    说明:本文仅供学习交流.转载请标明出处.欢迎转载!          vector容器是最简单的顺序容器,其用法相似于数组.实际上vector的底层实现就是採用动态数组.在编敲代码的过程中.经常会变量 ...

  7. HashMap有几种遍历方法?推荐使用哪种?

    本文已收录<面试精选>系列,Gitee 开源地址:https://gitee.com/mydb/interview HashMap 的遍历方法有很多种,不同的 JDK 版本有不同的写法,其 ...

  8. java 完全二叉树的构建与四种遍历方法

    本来就是基础知识,不能丢的太干净,今天竟然花了那么长的时间才写出来,记一下. 有如下的一颗完全二叉树: 先序遍历结果应该为:1  2  4  5  3  6  7 中序遍历结果应该为:4  2  5 ...

  9. 2017.10.25 Java List /ArrayList 三种遍历方法

    java list三种遍历方法性能比较 学习java语言list遍历的三种方法,顺便测试各种遍历方法的性能,测试方法为在ArrayList中插入记录,然后遍历ArrayList,测试代码如下: pac ...

随机推荐

  1. 6105 - deauth after EAPOL key exchange sequence

    wifi无法连接公司的网络 Warning Error in Event Log - deauth after EAPOL key exchange sequence https://forums.i ...

  2. C++的指针偏移

    假设一个类的定义如下:class Ob{public:Ob() : a(1), b(10) {}int a;private:int b; };

  3. Asynchronous C# server[转]

    It hasn't been thoroughly tested, but seems to work OK. This should scale pretty nicely as well. Ori ...

  4. 返回闭包不能引用循环变量,请改写count()函数,让它正确返回能计算1x1、2x2、3x3的函数。

    错误写法: 正确写法:

  5. Html5 学习笔记 --》css3 学习

    在开发任务中最好不要使用前缀 可以设置发散图形 圆形 方形等 边框图片效果: CSS3 变形效果: Css3 3D立体变形: css 设置 CSS3 过度效果: div:hover { backgro ...

  6. Visual Studio禁用IntelliSense

    通过Everything搜索feacp.dll,然后修改其名字或者直接删除之. 重启Visual Studio.

  7. PAT_A1073#Scientific Notation

    Source: PAT A1073 Scientific Notation (20 分) Description: Scientific notation is the way that scient ...

  8. docker使用记录一日常使用的命令

    docker官网 介绍docker的文档 https://docs.docker.com/install/linux/docker-ce/centos/ centos 安装docker 卸载cento ...

  9. “希希敬敬对”队软件工程第九次作业-beta冲刺第六次随笔

    “希希敬敬对”队软件工程第九次作业-beta冲刺第六次随笔 队名:  “希希敬敬对” 龙江腾(队长) 201810775001 杨希                   201810812008 何敬 ...

  10. shell编程:sed的选项

    sed [参数] [partern/commond] file 标准输出 | sed sed [参数] [partern/commond] -n :使用安静(silent)模式.在一般 sed 的用法 ...