C++ 和JAVA中的Foreach
foreach循环用于快速迭代容器(数组、向量等)的元素,而不执行初始化、测试或增加/减少。foreach循环通过为每个元素执行操作来工作,而不是执行n次操作。虽然C中没有foreach循环,但它受到C++和Java的支持。它首次在C++ 11中引入,并在Java的JDK 1.5.0中引入。在C++和Java中,foreach循环的关键字是”for”。
语法
for (data_type variable_name : container_type) {
operations using variable_name
}
由于C++引入了auto关键字,Java引入了var关键字,我们不再需要在foreach循环中指定变量的数据类型。类型推断会检测容器的数据类型,并将用于遍历的变量设置为相同的数据类型。
下面的代码演示了在不同的容器中使用foreach循环以及C++/Java中的auto/var关键字。
// C++ program to demonstrate use of foreach for array
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 10, 20, 30, 40 };
// Printing elements of an array using
// foreach loop
// Here, int is the data type, x is the variable name
// and arr is the array for which we want to iterate foreach
cout<<"Traversing the array with foreach using array's data type: ";
for (int x : arr)
cout<<x<<" ";
// data type of x is set as int
cout<<"\nTraversing the array with foreach using auto keyword : ";
for (auto x : arr)
cout<<x<<" ";
}
Java代码
// Java program to demonstrate use of foreach
public class Main {
public static void main(String[] args)
{
// Declaring 1-D array with size 4
int arr[] = { 10, 20, 30, 40 };
// Printing elements of an array using
// foreach loop
// Here, int is the data type, x is the variable name
// and arr is the array for which we want to iterate foreach
System.out.print("Traversing the array with foreach using array's data type: ");
for (int x : arr)
System.out.print(x+" ");
// data type of x is set as int
System.out.print("\nTraversing the array with foreach using auto keyword : ");
for (var x : arr)
System.out.print(x+" ");
}
}
输出
Traversing the array with foreach using array's data type: 10 20 30 40
Traversing the array with foreach using auto keyword : 10 20 30 40
Vector C++程序
#include
#include
using namespace std;
int main() {
vector value{"This", "is", "foreach", "example", "using", "vector."};
cout<<"Traversing the vector with foreach using vector's data type: ";
for (string v : value) {
cout<
输出
Traversing the vector with foreach using vector's data type: This is foreach example using vector.
Traversing the vector with foreach using auto keyword : This is foreach example using vector.
C++/Java集合程序
#include
#include
using namespace std;
int main() {
set value = {6, 2, 7, 4, 10, 5, 1};
cout<<"Traversing the set with foreach using set's data type: ";
for (int v : value) {
cout<
Java代码
import java.util.*;
public class GFG {
public static void main(String[] args)
{
Set hash_Set = new HashSet();
hash_Set.add("Geeks");
hash_Set.add("For");
hash_Set.add("Geeks");
hash_Set.add("Foreach");
hash_Set.add("Example");
hash_Set.add("Set");
System.out.print("Traversing the set with foreach using set's data type: ");
for(String hs : hash_Set) {
System.out.print(hs+" ");
}
System.out.print("\nTraversing the set with foreach using auto keyword : ");
for (var hs : hash_Set) {
System.out.print(hs+" ");
}
}
}
输出
Traversing the set with foreach using set's data type: 1 2 4 5 6 7 10
Traversing the set with foreach using auto keyword : 1 2 4 5 6 7 10
对于数组、向量和集合,我们可以在 foreach 中使用不同的数据类型。
C++/Java Map程序
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> mapExample;
mapExample.insert(pair<int, string>(1, "Geeks"));
mapExample.insert(pair<int, string>(2, "4"));
mapExample.insert(pair<int, string>(3, "Geeks"));
mapExample.insert(pair<int, string>(4, "Map"));
mapExample.insert(pair<int, string>(5, "Foreach"));
mapExample.insert(pair<int, string>(6, "Example"));
cout<<"Traversing the map with foreach using map's data type\n";
for (pair<int, string> mpEx : mapExample ) {
cout<<mpEx.first<<" "<<mpEx.second<<endl;
}
cout<<"\nTraversing the map with foreach using auto keyword\n";
for (auto mpEx : mapExample){
cout<<mpEx.first<<" "<<mpEx.second<<endl;
}
return 0;
}
Java代码
import java.io.*;
import java.util.Map;
import java.util.HashMap;
class GFG {
public static void main (String[] args) {
Map gfg = new HashMap();
gfg.put(1, "Geeks");
gfg.put(2, "4");
gfg.put(3, "Geeks");
gfg.put(4, "Map");
gfg.put(5, "Foreach");
gfg.put(6, "Example");
System.out.println("Traversing the map with foreach using map's data type");
for (Map.Entry entry : gfg.entrySet())
System.out.println(entry.getKey() + " " + entry.getValue());
System.out.println("\nTraversing the map with foreach using auto keyword");
for (var entry : gfg.entrySet())
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
输出
Traversing the map with foreach using map's data type
1 Geeks
2 4
3 Geeks
4 Map
5 Foreach
6 Example
Traversing the map with foreach using auto keyword
1 Geeks
2 4
3 Geeks
4 Map
5 Foreach
6 Example
Foreach循环具有以下优点
- 提高代码的可读性。
- 消除数据超出或不足的错误的可能性。
Foreach循环具有以下缺点
- 无法以倒序迭代元素。
- 将访问每个元素;无法跳过中间的元素。