To efficiently remove duplicate elements from a container using std::ranges::remove()
, you need to first ensure the container is sorted.
This is because std::ranges::remove()
works on contiguous elements and duplicates must be adjacent for this to work effectively.
Here's a step-by-step guide:
std::ranges::remove()
to shift the duplicates to the end.erase()
method to remove the surplus elements.Here's an example using a std::vector
:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> Source{3, 1, 2, 3, 2, 1, 4, 5};
// Step 1: Sort the container
std::sort(Source.begin(), Source.end());
// Step 2: Apply std::ranges::remove to shift duplicates
auto NewEnd = std::ranges::unique(Source);
// Step 3: Erase surplus elements
Source.erase(NewEnd.begin(), NewEnd.end());
// Display the result
std::cout << "Unique elements in Source: ";
for (auto Num : Source) {
std::cout << Num << ", ";
}
}
Unique elements in Source: 1, 2, 3, 4, 5,
In this example:
Source
vector to arrange the elements in ascending order.std::ranges::unique()
to move the duplicate elements to the end. Note that std::ranges::unique()
is more suitable for this task than std::ranges::remove()
, as it directly deals with adjacent duplicates.std::ranges::remove_if()
for Complex ConditionsIf you need to remove duplicates based on a more complex condition (e.g., user-defined types), you can use std::ranges::remove_if()
in combination with sorting:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
struct Player {
std::string Name;
int Score;
bool operator<(const Player& other) const {
return Score < other.Score; // Sort by Score
}
};
int main() {
std::vector<Player> Players{
{"Alice", 10}, {"Bob", 20},
{"Charlie", 10}, {"Diana", 20},
{"Eve", 30}
};
// Step 1: Sort the container
std::sort(Players.begin(), Players.end());
// Step 2: Apply std::ranges::unique to shift duplicates
auto NewEnd = std::ranges::unique(
Players,
[](const Player& a, const Player& b) {
return a.Score == b.Score;
}
);
// Step 3: Erase surplus elements
Players.erase(NewEnd.begin(), NewEnd.end());
// Display the result
std::cout << "Unique players by score:\n";
for (const auto& player : Players) {
std::cout << player.Name << " (Score "
<< player.Score << ")\n";
}
}
Unique players by score:
Alice (Score 10)
Bob (Score 20)
Eve (Score 30)
In this example:
Player
objects by their Score
.std::ranges::unique()
with a custom predicate to handle duplicates based on the Score
.erase()
method removes the surplus elements, resulting in a container with unique scores.By sorting the container and using std::ranges::unique()
, you can efficiently remove duplicates and maintain a clean, unique set of elements.
Answers to questions are automatically generated and may not have been reviewed.
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()
.