# Skip certain elements while Iterating over a Set in C++

This tutorial will discuss multiple ways to skip certain elements while iterating over a set in C++.

## Iterate over Set and skip elements based on Value

Suppose we have a set of numbers and we’re interested in iterating over only specific elements in the set, skipping the rest.

```std::set<int> numbers = {23, 45, 44, 21, 22, 60};
```

For instance, in this set of numbers, we might want to iterate only over the even numbers, while skipping the odd ones. To achieve this, we can use the for_each() function from the STL.

Initially, we’ll pass two iterators to for_each: one pointing to the start and the other to the end of the set. As the third argument, we’ll provide a lambda function. Inside this lambda, it accepts the value from the set, checks if the value is even, and if it is, prints it to the console. Odd numbers are simply skipped:

```// Skip all Odd Numbers while Iterating through Set
std::for_each(
numbers.begin(),
numbers.end(),
[](const int &value)
{
// Skip all the odd numbers from set
if (value % 2 == 0)
{
std::cout << value << " ";
}
});
```

Output:

```22 44 60
```

## Iterate over Set and skip elements based on Index Position

In the previous example, we selectively iterated through the set based on the value. However, what if we want to iterate based on index position, rather than the value?

To do this, you can still employ the for_each algorithm. Here’s how it works:

First, provide two iterators to for_each as before. Then, within your lambda function, accept the value from the set. In addition to this, maintain an external index value outside the lambda function. Within the lambda, you’ll check whether the current index position is even or not. If it’s even, you can print the value:

```// Skip elements at even index position
// while iterating through Set in C++
int index = 0;
std::for_each(
numbers.begin(),
numbers.end(),
[&](const int &value)
{
// Skip all alternative elements from set
if (index % 2 == 0)
{
std::cout << value << " ";
}
index++;
});
```

Output:

```21 23 45
```

This method allows you to iterate over the set and selectively process elements at even index positions when traversing the set in C++.

Let’s see the complete example,

```#include <iostream>
#include <set>
#include <algorithm>

int main()
{
std::set<int> numbers = {23, 45, 44, 21, 22, 60};

// Skip elements while iterating through a Set - based on Value

// Skip all Odd Numbers while Iterating through Set
std::for_each(
numbers.begin(),
numbers.end(),
[](const int &value)
{
// Skip all the odd numbers from set
if (value % 2 == 0)
{
std::cout << value << " ";
}
});
std::cout << std::endl;

// Skip elements while iterating through a Set - based on Index Position

// Skip elements at even index position
// while iterating through Set in C++
int index = 0;
std::for_each(
numbers.begin(),
numbers.end(),
[&](const int &value)
{
// Skip all alternative elements from set
if (index % 2 == 0)
{
std::cout << value << " ";
}
index++;
});
std::cout << std::endl;

return 0;
}
```

Output

```22 44 60
21 23 45
```

Summary

Today, we learned about skip certain elements while iterating over a set in C++.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top