Removal Algorithms

Remove with Multiple Conditions

How can I remove elements from a container based on multiple conditions using std::remove_if()?

Abstract art representing computer programming

To remove elements from a container based on multiple conditions using std::remove_if(), you can define a predicate function that encapsulates these conditions.

The predicate function will return true if an element should be removed.

Here’s an example using a std::vector<int> where we want to remove elements that are either less than 2 or greater than 5:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  std::vector<int> Source{1, 2, 3, 4, 5, 6, 7};

  // Define the predicate with multiple conditions
  auto Predicate = [](int x) {
    return x < 2 || x > 5;
  };

  // Apply std::remove_if with the predicate
  auto NewEnd = std::remove_if(
    Source.begin(), Source.end(), Predicate);

  // Erase the surplus elements
  Source.erase(NewEnd, Source.end());

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

In this example:

  • We define a lambda function Predicate that returns true if an element is less than 2 or greater than 5.
  • We pass this predicate to std::remove_if(), which moves the elements that meet the conditions to the end of the container and returns an iterator to the new logical end.
  • We then use the erase() method to remove the surplus elements from the container.

This approach is flexible and allows you to define any combination of conditions within the predicate function. You can easily adapt this method to other containers and more complex conditions.

For example, you could remove elements from a container of user-defined types based on multiple member variables:

#include <algorithm>
#include <iostream>
#include <vector>

class Player {
 public:
  Player(std::string name, int score)
    : Name{name}, Score{score} {}

  std::string Name;
  int Score;
};

int main() {
  std::vector<Player> Players{
    {"Alice", 10}, {"Bob", 5},
    {"Charlie", 7}, {"Diana", 2}
  };

  // Define the predicate for multiple conditions
  auto Predicate = [](const Player& p) {
    return p.Score < 5 || p.Score > 8;
  };

  // Apply std::remove_if with the predicate
  auto NewEnd = std::remove_if(Players.begin(),
    Players.end(), Predicate);

  // Erase the surplus elements
  Players.erase(NewEnd, Players.end());

  // Display the result
  std::cout << "Filtered players: ";
  for (const auto& p : Players) {
    std::cout << p.Name << " (Score "
      << p.Score << "), ";
  }
}
Filtered players: Bob (Score 5), Charlie (Score 7),

This demonstrates how you can use std::remove_if() with complex conditions tailored to your specific use case.

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

A computer programmer
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:

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

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved