-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestUniqueErase.cpp
More file actions
33 lines (26 loc) · 1008 Bytes
/
testUniqueErase.cpp
File metadata and controls
33 lines (26 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <algorithm>
#include <iostream>
#include <ranges>
#include <vector>
void print(const std::vector<int>& v, const std::string_view label)
{
std::cout << label << " { ";
for (const int x : v) std::cout << x << ' ';
std::cout << "}\n";
}
int main()
{
// ----- 1. make a vector that contains duplicates -------------- //
std::vector data { 7, 2, 9, 2, 7, 7, 4, 9, 1, 4 };
print(data, "raw ");
// ----- 2. sort (needed because unique removes *consecutive* dups) //
std::ranges::sort(data);
print(data, "sorted ");
// ----- 3. unique + erase idiom ---------------------------------- //
const auto newEnd = std::ranges::unique(data).begin(); // step A
data.erase(newEnd, data.end()); // step B
print(data, "deduped ");
// ----- 4. sanity check ------------------------------------------ //
if (const std::vector expected { 1, 2, 4, 7, 9 }; data != expected)
std::cerr << "Error: dedup failed!\n";
}