使用for-each循环遍历容器中的每个元素
c++11的新特性,v是一个可遍历的容器或流,比如vector类型,i就用来在遍历过程中获得容器里的每一个元素。
for(auto i:v)
for(auto &i:v)
代码1:
#include<iostream>
#include<string>
using namespace std;
string s = "hello";
for (auto &i : s ) //i是个引用 i到底引用的是什么?
i = toupper(i); //改变成大写,影响s的值
cout<<s<<endl; //s的值是 HELLO
--------------------------------------------------------------
代码2:
#include<iostream>
#include<string>
using namespace std;
string s = "hello";
for (auto i : s ) //书上说i 是char类型,那s[n]呢?
i = toupper(i); //改变成大写,不影响s的值
cout<<s<<endl; //s的值是 hello
代码1:
#include<iostream>
#include<string>
using namespace std;
string s = "hello";
for (auto &i : s ) //i是个引用 i到底引用的是什么?
i = toupper(i); //改变成大写,影响s的值
cout<<s<<endl; //s的值是 HELLO
--------------------------------------------------------------
代码2:
#include<iostream>
#include<string>
using namespace std;
string s = "hello";
for (auto i : s ) //书上说i 是char类型,那s[n]呢?
i = toupper(i); //改变成大写,不影响s的值
cout<<s<<endl; //s的值是 hello
c++中for(auto count : counts)
意思是将 counts 容器中的每一个元素从前往后枚举出来,并用 count 来表示,类似于Java中的 for each 语句,举个栗子:
1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 int main() { 5 int a[] = { 1,2,3,5,2,0 }; 6 vector<int>counts(a,a+6); 7 for (auto count : counts) 8 cout<< count<< " "; 9 cout << endl; 10 return 0; 11 }
运行的效果是:
这是C++11中的语法,即:Range-based for loop。其中counts应满足:begin(counts), end(counts)是合法的。
因此,它等价于for(some_iterator p = begin(counts); p != end(counts); ++p)且some_type count = *p。
另外还可以是for(auto& count : counts), for(auto&& count: counts)。
它们的区别在于count是值还是引用。
最后,在c++14中还允许for(count : counts),等价于for(auto&& count: counts)。
【转载自】
auto关键字:for(auto &i:s)和for(auto i:s) - F~C~H的博客 - ****博客 https://blog.****.net/qq_34037046/article/details/85221622
c++中for(auto count : counts)是什么意思意思_百度知道 https://zhidao.baidu.com/question/1861076889396870787.html
C++11 之for 新解 auto - Jerry_Jin - 博客园 https://www.cnblogs.com/jins-note/p/9513129.html
浅析C语言auto关键字和C++ 中的auto关键字 - Keep Fighting All The Time - ****博客 https://blog.****.net/LiuBo_01/article/details/80752734
【C++11新特性】 auto关键字 - 老董 - 博客园 https://www.cnblogs.com/lenmom/p/7988635.html
推荐阅读
-
如何使用迭代器/反向迭代器删除遍历中的元素
-
Java 类加载器的作用 - 简介:类加载器是 Java™ 中一个非常重要的概念。类加载器负责将 Java 类的字节码加载到 Java 虚拟机中。本文首先详细介绍了 Java 类加载器的基本概念,包括代理模型、加载类的具体过程和线程上下文类加载器等。然后介绍了如何开发自己的类加载器,最后介绍了类加载器在 Web 容器和 OSGi™ 中的应用。 类加载器是 Java 语言的一项创新,也是 Java 语言广受欢迎的重要原因之一。它允许将 Java 类动态加载到 Java 虚拟机中并执行。类加载器从 JDK 1.0 开始出现,最初是为了满足 Java Applets 的需求而开发的,Java Applets 需要从远程位置下载 Java 类文件并在浏览器中执行。现在,类加载器已广泛应用于网络容器和 OSGi。一般来说,Java 应用程序的开发人员不需要直接与类加载器交互;Java 虚拟机的默认行为足以应对大多数情况。但是,如果遇到需要与类加载器交互的情况,而您又不太了解类加载器的机制,就很容易花费大量时间调试异常,如 ClassNotFoundException 和 NoClassDefFoundError。本文将详细介绍 Java 的类加载器,帮助读者深入理解 Java 语言中的这一重要概念。下面先介绍一些基本概念。 类加载器的基本概念 顾名思义,类加载器用于将 Java 类加载到 Java 虚拟机中。一般来说,Java 虚拟机以如下方式使用 Java 类:Java 源程序(.java 文件)经 Java 编译器编译后转换为 Java 字节代码(.class 文件)。类加载器负责读取 Java 字节代码并将其转换为 java.lang 实例。每个实例都用来表示一个 Java 类。通过该实例的 newInstance 方法创建该类的对象。实际情况可能更加复杂,例如,Java 字节代码可能是由工具动态生成或通过网络下载的。 基本上,所有类加载器都是 java.lang.ClassLoader 类的实例。下面将详细介绍这个 Java 类。 java.lang.ClassLoader 类简介 java.lang.ClassLoader 类的基本职责是根据给定类的名称为其查找或生成相应的字节码,然后根据这些字节码定义一个 Java 类,即 java.lang.Class 类的实例。除此之外,ClassLoader 还负责加载 Java 应用程序所需的资源,如图像文件和配置文件。不过,本文只讨论它加载类的功能。为了履行加载类的职责,ClassLoader 提供了许多方法,其中比较重要的方法如表 1 所示。下文将详细介绍这些方法。 表 1.与加载类相关的 ClassLoader 方法
-
C#中的数组以及如何使用foreach循环进行遍历
-
使用for-each循环遍历容器中的每个元素
-
使用 for in 循环遍历 JSON 中的键值对
-
动态规划算法的两种经典解决方式:最优子结构和DP数组的使用解析-动态规划算法问题 什么叫作最优子结构? 和动态规划有什么关系? 为什么动态规划遍历DP数组的方式有正着遍历,有倒着遍历,有斜着遍历? 最优子结构 最优子结构是某些问题的一种特定的性质,并不是动态规划问题所特有的. 很多问题都具有最优子结构,但是其中大部分不具有重叠子问题,所以不会归为动态规划系列的问题 最优子结构: 可以从子问题的最优结果推导出更大规模问题的最优结果 子问题之间必须相互独立 通过改造问题来优化由于子问题之间不独立而导致的最优子结构失效的情况: 问题: 假设学校有10个班,已知每个班的最高分与最低分差值的最大分数差,需要计算全校学生中的最大分数差 分析: 这样的问题就无法通过这10个班的最大分数差来推导出来,因为这10个班的最大分数差不一定就包含全校学生的最大分数差.比如全校的最大分数差可能是由8班的最高分和6班的最低分的分数差而得.这样就导致子问题之间不是互相独立的 改造问题: 直接进行问题改造 int result = 0; for (Student a : school) { for (Student b : school) { if (a is b) { continue; } result = max(result, |a.score - b.score|) } } return result; 改造问题就是将问题等价转化: 最大分数差就等价于最高分数与最低分数的差 那么就是要求最高和最低分数 求最高分数是具备最优子结构的,求最低分数也是具有最优子结构的 这样就样一个不具备最优子结构的问题转化为具备最优子结构的子问题 借助最优子结构解决最值问题,再解决最大分数差问题 题目: 求一棵二叉树的最大值,假设节点中的值都为非负数 int maxVal(TreeNode root) { if (root == null) { return -1; } int left = maxVal(root.left); int right = maxVal(root.right); return max(root.val, left, right); }