The for-each
loop (also known as range-based for
loop) in C++ is a simpler and more convenient way to iterate over elements in containers like arrays, vectors, or other collections. It was introduced in C++11 to make looping through elements more intuitive and readable.
This type of loop iterates over each element in a container without explicitly using an index.
1️⃣ Syntax of for-each
Loop:
for (datatype element : container) {
// Code to process each element
}
- datatype: The type of the element in the container (e.g.,
int
,char
,float
). - element: A temporary variable that represents each individual element in the container during each iteration.
- container: The collection you are iterating over (e.g., an array, vector, or any other container).
2️⃣ Example: for-each
Loop with Array
Here’s an example of using a for-each
loop to print all elements of an array:
#include <iostream>
using namespace std;
int main() {
int numbers[] = {1, 2, 3, 4, 5};
// for-each loop to print each element in the array
for (int num : numbers) {
cout << num << " ";
}
return 0;
}
Explanation:
- The loop iterates over each element in the
numbers
array, andnum
takes the value of each element one by one. - The elements are printed out without needing to manage the index manually.
Output:
1 2 3 4 5
3️⃣ Example: for-each
Loop with Vector
The for-each
loop works not just with arrays, but also with other containers such as vector
. Here's an example using a vector
:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {10, 20, 30, 40, 50};
// for-each loop to print each element in the vector
for (int num : numbers) {
cout << num << " ";
}
return 0;
}
Explanation:
- This example demonstrates iterating over a
vector
container using thefor-each
loop. - The loop goes through each element of the vector and prints it.
Output:
10 20 30 40 50
4️⃣ Example: for-each
Loop with String
You can also use the for-each
loop to iterate over characters in a string
:
#include <iostream>
#include <string>
using namespace std;
int main() {
string text = "Hello";
// for-each loop to print each character in the string
for (char c : text) {
cout << c << " ";
}
return 0;
}
Explanation:
- In this case, the loop iterates through each character in the string
text
, printing them one by one.
Output:
H e l l o
5️⃣ Using const
with for-each
Loop
If you do not need to modify the elements while iterating, it's a good practice to use the const
keyword to ensure the elements are not accidentally changed:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {10, 20, 30, 40, 50};
// for-each loop with const to avoid modifying elements
for (const int num : numbers) {
cout << num << " ";
}
return 0;
}
Explanation:
- Here,
const int num
ensures that the elements of the vector cannot be modified within the loop. - This is useful when you want to guarantee that the elements remain unchanged.
Output:
10 20 30 40 50
6️⃣ Using Reference in for-each
Loop
If you want to modify the elements of the container within the loop, you can use references:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {10, 20, 30, 40, 50};
// for-each loop with reference to modify elements
for (int& num : numbers) {
num *= 2; // Multiply each element by 2
}
// Print modified elements
for (int num : numbers) {
cout << num << " ";
}
return 0;
}
Explanation:
- The
int& num
ensures that the loop variablenum
is a reference to the actual element in the container. - This allows you to modify the elements inside the loop (in this case, multiplying them by 2).
Output:
20 40 60 80 100
7️⃣ Limitations of for-each
Loop:
- No index access: Unlike traditional
for
loops, you cannot access the index of the current element directly in afor-each
loop. - Non-modifiable elements: If you use
const
, you cannot modify the container's elements. - Works only with containers: The
for-each
loop can only be used with iterable containers, like arrays, vectors, or lists.
8️⃣ Key Points to Remember:
- The
for-each
loop is simpler and cleaner than traditionalfor
loops when you don’t need to use the index. - It's ideal for iterating over containers (arrays, vectors, sets, maps, etc.) without manually managing indices.
- You can use references if you need to modify elements within the loop.
Would you like to see more advanced examples, such as iterating over a map or performing specific actions on elements during iteration?
0 Comments