Remove Copy with Custom Comparator

Is there a way to use remove_copy_if() with a custom comparator function?

std::remove_copy_if() is designed to exclude elements from copying based on a predicate, but it doesn't directly support custom comparators like std::sort() does.

However, you can achieve similar functionality by designing your predicate function to encapsulate your comparison logic.

Here's how you can use std::remove_copy_if() with a custom comparator-like logic within the predicate:

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

struct Player {
  std::string Name;
  int Score;
};

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

  std::vector<Player> Destination(Players.size());

  // Define the predicate function
  auto CustomComparator = [](const Player& p) {
    // Custom logic: exclude players with score < 5
    return p.Score < 5;
  };

  // Apply std::remove_copy_if with the predicate
  auto Result = std::remove_copy_if(
      Players.begin(), Players.end(),
      Destination.begin(), CustomComparator);

  // Adjust the size of the destination vector
  Destination.erase(Result, Destination.end());

  // Display the result
  std::cout << "Copied players:\n";
  for (const auto& p : Destination) {
    std::cout << p.Name << " (Score "
      << p.Score << ")\n";
  }
}
Copied players:
Alice (Score 10)
Bob (Score 5)
Charlie (Score 7)

In this example:

  • We define a predicate CustomComparator to exclude players with a score less than 5.
  • We use std::remove_copy_if() to copy elements from the source to the destination, excluding those that meet the condition defined by the predicate.
  • After copying, we adjust the size of the destination vector to remove the unused elements.

By embedding the custom comparison logic within the predicate function, you can effectively use std::remove_copy_if() to exclude elements based on complex conditions.

This method is flexible and can be adapted to various types of custom comparisons and conditions, providing a powerful tool for container manipulation in C++.

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

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Remove Elements Without Surplus
How can I remove elements from a container without leaving surplus elements at the end?
Remove vs Erase
What is the difference between std::remove() and std::erase() in C++?
Memory Management with std::remove()
How do I handle memory management when using std::remove() with dynamic arrays?
Remove and Erase in std::map
How do I remove elements from a std::map using the remove-erase idiom?
Remove with Multiple Conditions
How can I remove elements from a container based on multiple conditions using std::remove_if()?
Remove Elements from Ordered Containers
How do I remove elements from a std::set while maintaining its sorted property?
Remove and Erase in std::deque
How do I remove elements from a std::deque using the remove-erase idiom?
Efficiently Remove Duplicates
How do I efficiently remove duplicate elements from a container using std::ranges::remove()?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant