Removal Algorithms

# Remove Elements from Ordered Containers

## How do I remove elements from a std::set while maintaining its sorted property?

std::set is an ordered container, and removing elements from it while maintaining the sorted property can be done using the erase()Â method.

Since std::set automatically maintains order, you don't need to do anything special to keep it sorted afterÂ removal.

Hereâ€™s an example of how to remove elements from a std::set:

#include <iostream>
#include <set>

int main() {
std::set<int> Numbers{1, 2, 3, 4, 5, 6, 7, 8};

// Remove elements less than 3 or greater than 6
for (auto it = Numbers.begin(); it != Numbers.end();) {
if (*it < 3 || *it > 6) {
it = Numbers.erase(it);
} else {
++it;
}
}

// Display the result
std::cout << "Filtered elements in Numbers: ";
for (auto Num : Numbers) {
std::cout << Num << ", ";
}
}
Filtered elements in Numbers: 3, 4, 5, 6,

In thisÂ example:

• We iterate over the std::set using an iterator.
• The erase() method is used to remove elements that are less than 3 or greater than 6.
• The iterator is properly adjusted after each erase() call to ensure it remains valid.

Since std::set is always sorted, the remaining elements will still be in ascending order after the removalÂ operation.

### Removing Elements Based on Multiple Conditions

You can also use multiple conditions to remove elements from a std::set. Hereâ€™s an example where we remove elements that are either odd or greater thanÂ 6:

#include <iostream>
#include <set>

int main() {
std::set<int> Numbers{1, 2, 3, 4, 5, 6, 7, 8};

// Remove odd elements or elements greater than 6
for (auto it = Numbers.begin(); it != Numbers.end();) {
if (*it % 2 != 0 || *it > 6) {
it = Numbers.erase(it);
} else {
++it;
}
}

// Display the result
std::cout << "Filtered elements in Numbers: ";
for (auto Num : Numbers) {
std::cout << Num << ", ";
}
}
Filtered elements in Numbers: 2, 4, 6,

By using the erase() method and iterating through the std::set, you can effectively remove elements based on any condition while maintaining the container's inherent sortedÂ property.

This method is efficient and leverages the ordered nature of std::set.

This Question is from the Lesson:

### Removal Algorithms

An overview of the key C++ standard library algorithms for removing objects from containers. We cover remove(), remove_if(), remove_copy(), and remove_copy_if().

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

### Removal Algorithms

An overview of the key C++ standard library algorithms for removing objects from containers. We cover remove(), remove_if(), remove_copy(), and remove_copy_if().

Part of the course:

## Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

### This course includes:

• 124 Lessons
• 550+ Code Samples
• 96% Positive Reviews
• Regularly Updated
• Help and FAQ
Free, Unlimited Access

### Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.