这篇文章整理了algorithm.
#include <algorithm>
set operation
| Algorithm | Meaning | explanation |
|---|---|---|
std::set_union |
A ∪ B | all elements from A and B |
std::set_intersection |
A ∩ B | elements common to both |
std::set_difference |
A − B | elements in A but not B |
std::set_symmetric_difference |
(A − B) ∪ (B − A) | elements in A or B but not both |
common requirements:
- Inputs must be sorted
- Same comparator must be used
- Output range must be large enough
- Input ranges must not overlap with output
other
std::unique
template <class ForwardIterator>
ForwardIterator unique(ForwardIterator first, ForwardIterator last);
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
- In-Place Operation:
std::uniquedoes not resize the container or physically remove elements. It simply shifts the unique elements to the front. - Range Assumption: The range should be sorted if you want to remove all duplicates. Otherwise, only consecutive duplicates are removed.
- Reclaim Space: If used on a container like
std::vector, you need to call erase to remove the redundant elements physically.
an example
#include <iostream>
#include <vector>
#include <algorithm>
bool almost_equal(double a, double b) {
return std::abs(a - b) < 0.1;
}
int main() {
std::vector<double> vec = {1.01, 1.02, 2.0, 2.05, 3.0};
// Remove consecutive "almost equal" duplicates
auto new_end = std::unique(vec.begin(), vec.end(), almost_equal);
vec.erase(new_end, vec.end());
for (double num : vec) {
std::cout << num << " ";
}
return 0;
}
std::max_element
#include <iostream>
#include <vector>
#include <algorithm> // for std::max_element
// Custom compare function (finds max by absolute value)
bool compareAbs(int a, int b) {
return std::abs(a) < std::abs(b);
}
int main() {
std::vector<int> values = {-10, 3, -7, 9, -2, 8};
{
// Find the element with the largest absolute value
auto max_it = std::max_element(values.begin(), values.end(), compareAbs);
if (max_it != values.end()) {
std::cout << "Maximum value by absolute magnitude: " << *max_it << std::endl;
} else {
std::cout << "Vector is empty" << std::endl;
}
}
{ // Find the element with the largest absolute value
auto max_it = std::max_element(values.begin(), values.end(),
[](int a, int b) { return std::abs(a) < std::abs(b); });
}
return 0;
}