logo

C++ および Java での Foreach

Foreach ループ 初期化、テスト、およびインクリメント/デクリメントを実行せずに、コンテナーの要素 (配列、ベクトルなど) を迅速に反復するために使用されます。 foreach ループの動作は、何かを n 回実行するのではなく、すべての要素に対して何かを実行することです。 C には foreach ループはありませんが、C++ と Java は両方とも foreach タイプのループをサポートしています。 C++ では C++ 11 で導入され、Java では JDK 1.5.0 で導入されました。 foreach ループに使用されるキーワードは次のとおりです。 のために C++ と Java の両方で。

構文:



for (data_type variable_name : container_name) { operations using variable_name }>

C++ の auto キーワードと Java の var キーワードの導入により、foreach ループで変数のデータ型を指定する必要がなくなりました。型推論はコンテナのデータ型を検出し、トラバースに使用される変数に同じデータ型を自動的に設定します。

以下のコードは、さまざまなコンテナーの foreach ループの使用例を示しています。 オート/リム のキーワード C++/Java

C++/Java プログラム 配列の場合:



C++






// C++ program to demonstrate use of foreach for array> #include> 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<' '; // data type of x is set as int cout<<' Traversing the array with foreach using auto keyword : '; for (auto x : arr) cout<' '; }>

>

>

ジャワ




// 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(>' Traversing 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>

ベクトル用の C++ プログラム:

C++


2次元配列用の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 << v <<>' '>;> >}> >cout <<>' Traversing the vector with foreach using '> >'auto keyword : '>;> >for> (>auto> v : value)> >cout << v <<>' '>;> >return> 0;> }>

ふふ

>

>

出力

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.>

ArrayList 用の Java プログラム:

ジャワ




/*package whatever //do not write package name here */> import> java.util.*;> class> GFG {> >public> static> void> main(String[] args)> >{> >ArrayList list =>new> ArrayList();> >list.add(>3>);> >list.add(>24>);> >list.add(->134>);> >list.add(->2>);> >list.add(>100>);> >for> (>int> element : list) {> >System.out.print(element +>' '>);> >}> >}> }>

>

>

出力

3 24 -134 -2 100>

セット用の C++/Java プログラム:

C++




#include> #include> using> namespace> std;> int> main() {> >set<>int>>値 = {6, 2, 7, 4, 10, 5, 1};>>' >cout<<>'Traversing the set with foreach using set's data type: '>;> >for> (>int> v : value) {> >cout<' '; } cout<<' Traversing the set with foreach using auto keyword : '; for (auto v : value) cout<' '; return 0; }>

>

>

ジャワ




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(>' Traversing 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 プログラム:

C++14




#include> #include> using> namespace> std;> int> main() {> >map<>int>, string>マップの例;>>' mapExample.insert(pair<>int>, string>(1,>>));> >mapExample.insert(pair<>int>, string>(2,>>));> >mapExample.insert(pair<>int>, string>(3,>>));> >mapExample.insert(pair<>int>, string>(4,>>));> >mapExample.insert(pair<>int>, string>(5,>>));> >mapExample.insert(pair<>int>, string>(6,>>));> > >cout<<>'Traversing the map with foreach using map's data type '>;> >for> (pair<>int>, string>mpEx : マップ例 ) {> >cout<' '< } cout<<' Traversing the map with foreach using auto keyword '; for (auto mpEx : mapExample){ cout<' '< } 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(>' Traversing 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 ループの欠点:

  • 要素を逆の順序で反復することはできません。
  • それぞれの要素にアクセスしますが、間の要素をスキップすることはできません。