1、代码如下:

void output1(int x)
{
if (x == 10000000)
{
std::cout << x << std::endl;
}

}
const std::string getCurrentSystemTime()
{
auto tt = std::chrono::system_clock::to_time_t
(std::chrono::system_clock::now());
struct tm* ptm = localtime(&tt);
char date[60] = { 0 };
sprintf(date, "%d-%02d-%02d %02d:%02d:%02d",
(int)ptm->tm_year + 1900, (int)ptm->tm_mon + 1, (int)ptm->tm_mday,
(int)ptm->tm_hour, (int)ptm->tm_min, (int)ptm->tm_sec);
return std::string(date);
}
void Test9()
{
std::cout << getCurrentSystemTime() << std::endl;
std::vector<int> coll;
for (int i = 0; i <= 10000000; i++)
{
coll.push_back(i);
}
std::cout<<getCurrentSystemTime()<<std::endl;
std::for_each(coll.begin(), coll.end(), output1);
std::cout << getCurrentSystemTime() << std::endl;
for (auto iter : coll)
{
if (iter == 10000000)
{
std::cout << iter << std::endl;
}
}
std::cout << getCurrentSystemTime() << std::endl;
for (auto iter = coll.begin(); iter != coll.end(); ++iter)
{
if (*iter == 10000000)
{
std::cout << *iter << std::endl;
}
}
std::cout << getCurrentSystemTime() << std::endl;

for (auto iter = coll.begin(); iter != coll.end(); iter++)
{
if (*iter == 10000000)
{
std::cout << *iter << std::endl;
}
}
std::cout << getCurrentSystemTime() << std::endl;
}

2、运行结果如下:

C++基于范围的for循环性能测试(针对std::vector)的更多相关文章

  1. C++11基于范围的for循环

    C++11包含一种新的 for 循环,称为基于范围的 for 循环,可以简化对数组元素的遍历.格式如下: for(Type VarName : Array){ //每个元素的值会依次赋给 VarNam ...

  2. Atitit.升级软件的稳定性---基于数据库实现持久化 循环队列 循环队列

    Atitit.升级软件的稳定性---基于数据库实现持久化  循环队列 环形队列 1. 前言::选型(马) 1 2. 实现java.util.queue接口 1 3. 当前指针的2个实现方式 1 1.1 ...

  3. C++ 11 学习2:空指针(nullptr) 和 基于范围的for循环(Range-based for loops)

    3.空指针(nullptr) 早在 1972 年,C语言诞生的初期,常数0带有常数及空指针的双重身分. C 使用 preprocessor macroNULL 表示空指针, 让 NULL 及 0 分别 ...

  4. 基于范围的for循环(STL)

    1. ]={4.99,5.99,6.99,7.99,8.99}; for (double x : prices) cout<<x<<endl; //////////////// ...

  5. 基于范围的for循环(C++11)

    C++11新增了一种循环:基于范围的for循环.这简化了一种常见的循环任务:对数组(或容器类,如vector和array)的每个元素执行相同的操作,如下例所示 for语句允许简单的范围迭代:(只遍历, ...

  6. 第9课 基于范围的for循环

    1. 基于范围的for循环(range-based for) (1)语法:for(decl : coll){//statement} ①decl用于声明元素及类型,如int elem或auto ele ...

  7. c++11 基于范围的for循环

    c++11 基于范围的for循环 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> # ...

  8. c++11——基于范围的for循环

    c++11中有基于范围的for循环,基于范围的for循环可以不再关心迭代器的概念,只需要关系容器中的元素类型即可,同时也不必显式的给出容器的开头和结尾. int arr[] = {1, 2, 3, 4 ...

  9. c++11之一: 基于范围的for循环

    #include <iostream> using namespace std; int main(){ ]{,,,,}; for (int& e: ary) e *= ; for ...

随机推荐

  1. C#实时检测端口占用情况

    在TCP/IP协议中,服务端需要去监听客户端的端口,开始监听,我们需要检测使用的端口是否被占用,获取系统当前使用的所有端口号,用此端口进行匹配即可. 代码如下 internal static Bool ...

  2. “全栈2019”Java多线程第二十九章:可重入锁与不可重入锁详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  3. 线性表java实现

    顺序表 public class SequenceList { /* * content,节点内容 * location,节点在表中的位置(序号) * */ private String conten ...

  4. Ubuntu系统的安装(虚拟机) 并配置C/C++编译器

    一.系统的初始化配置 1.配置静态IP和DNS 配置静态IP 1.sudo vim /etc/network/interfaces,修改文件内容如下: auto eth0  #表示让网卡开机自动挂载e ...

  5. POJ 2725

    #include <iostream> #include <string> #include <algorithm> #define MAXN 400005 usi ...

  6. Java java.lang.ExceptionInInitializerError 错误解决方案

    引起 java.lang.ExceptionInInitializerError 错误的原因是:在类的初始化时,出错.也就是说,在加载类时,执行static的属性.方法块时,出错了. 比如 publi ...

  7. opencv2函数学习之erode、dilate:图像腐蚀和膨胀

    图像腐蚀和图像膨胀是图像中两种最基本形态学操作. ,-), ,int borderType=BORDER_CONSTANT, const Scalar& borderValue=morphol ...

  8. (转)python协程2:yield from 从入门到精通

    原文:http://blog.gusibi.com/post/python-coroutine-yield-from/ https://mp.weixin.qq.com/s?__biz=MzAwNjI ...

  9. Git使用(3)

    1.查看本地和远程分支 git branch -a 删除本地分支 git branch -D branchName(D要大写) 删除远程分支 git push origin :branchName 2 ...

  10. Android使用Xutil3.0下载文件.md

    Android使用Xutil3.0下载文件.md 新建项目: 引入依赖: implementation 'org.xutils:xutils:3.5.0' 添加权限: <uses-permiss ...