在for(int val:arr)中,冒号是什么意思? [英] In for (int val :arr), what does the colon mean?
本文介绍了在for(int val:arr)中,冒号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <iostream>
using namespace std;
int arr[] = { 1, 2, 3 };
void Show()
{
for (int val : arr) {
cout << val;
}
}
int main()
{
Show();
return 0;
}
结果将为123。
我将非常感谢您对此解释进行解释-int val:arr-
具体是什么-:-在这里吗?这是什么。
result will be 123 . i will be very grateful to get explanation of this -int val :arr- specifically what - : - does here ? what is it .
推荐答案
它称为范围循环。
int arr[] = { 1, 2, 3 };
for (int val : arr)
cout << val;
与以下内容相同:
int arr[] = { 1, 2, 3 };
for (int i=0;i<sizeof(arr)/sizeof(int);i++)
{
int val=arr[i];
cout << val;
}
但是范围循环可以做得更多。
But range loop can do far more.
这只是意味着遍历所有成员。请记住,某些类可能具有复杂的迭代器,并且的范围$
以一种干净的方式遍历数组。另外,请注意,它是 c ++ 11
功能。
It simply means loop through all members. Keep in mind some classes can have a complicated iterator and a range for
runs through an array in a clean way. Also, notice it is a c++11
feature.
这篇关于在for(int val:arr)中,冒号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文