32
loading...
This website collects cookies to deliver better user experience
includes
set_difference
set_intersection
set_symmetric_difference
set_union
std::set
.std::includes
in its simplest form takes 4 parameters, 4 iterators. The first two defining one range, and the second two another range.true
in particular if the second range is a subsequence of the first one.#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector nums {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector subsequece {3, 4, 5};
std::vector subset {5, 4, 3};
std::vector otherNums {42, 51, 66};
std::cout << std::boolalpha;
std::cout << "std::includes(nums.begin(), nums.end(), subsequece.begin(), subsequece.end()): " << std::includes(nums.begin(), nums.end(), subsequece.begin(), subsequece.end()) << '\n';
std::cout << "std::includes(nums.begin(), nums.end(), subset.begin(), subset.end()): " << std::includes(nums.begin(), nums.end(), subset.begin(), subset.end()) << '\n';
std::cout << "std::includes(nums.begin(), nums.end(), otherNums.begin(), otherNums.end()): " << std::includes(nums.begin(), nums.end(), otherNums.begin(), otherNums.end()) << '\n';
}
/*
std::includes(nums.begin(), nums.end(), subsequece.begin(), subsequece.end()): true
std::includes(nums.begin(), nums.end(), subset.begin(), subset.end()): false
std::includes(nums.begin(), nums.end(), otherNums.begin(), otherNums.end()): false
*/
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector nums {1, 2, 5, 4, 3, 6, 7, 8, 9, 10};
std::vector subseq {5, 4, 3};
std::cout << std::boolalpha;
std::cout << "std::includes(nums.begin(), nums.end(), subseq.begin(), subseq.end()): " << std::includes(nums.begin(), nums.end(), subseq.begin(), subseq.end()) << '\n';
}
/*
std::includes(nums.begin(), nums.end(), subseq.begin(), subseq.end()): true
*/
std::includes
was able to find a subsequence in it. Yet, you should not rely on this. If you don't pass sorted ranges to std::includes
, the behaviour is undefined.std::includes
can take two extra parameters, I'd say the usual ones.set_difference
is only guaranteed to work with sorted ranges.std::set_difference
can also take the usual two extra parameters, like an execution policy before all the others or a comparator after all the parameters.#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector nums {1, 2, 3, 4, 5, 5};
std::vector otherNums {1, 2, 3, 6, 7};
std::vector<int> difference;
std::set_difference(nums.begin(), nums.end(),
otherNums.begin(), otherNums.end(),
std::back_inserter(difference));
for (auto n : difference) {
std::cout << n << " ";
}
std::cout << '\n';
}
/*
4 5 5
*/
5
twice in nums
and not at all in otherNums
, so it appears twice in difference
. But if 5
appears once in otherNums
too, it will still appear in the difference
, but then only once. After all, that's the difference. If it appears twice in the first input and only once in the second, that is the difference.set_intersection
takes the same parameters as set_difference
.m
times and n
times in the second, it will be copied std::min(m,n)
times.std::set_intersection
also keeps the items in their relative order, the order of the items in the input and in the output range is the same.#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector nums {1, 2, 3, 4, 5};
std::vector sameNums {1, 2, 3, 4, 5};
std::vector otherNums {1, 2, 7};
std::vector<int> intersectionOfSame;
std::vector<int> otherIntersection;
std::set_intersection(nums.begin(), nums.end(),
sameNums.begin(), sameNums.end(),
std::back_inserter(intersectionOfSame));
for (auto n : intersectionOfSame) {
std::cout << n << " ";
}
std::cout << '\n';
std::set_intersection(nums.begin(), nums.end(),
otherNums.begin(), otherNums.end(),
std::back_inserter(otherIntersection));
for (auto n : otherIntersection) {
std::cout << n << " ";
}
std::cout << '\n';
}
/*
1 2 3 4 5
1 2
*/
set_symmetric_difference
still operates on the very same list of parameters as our previous two algorithms.std::set_difference
, with the input ranges swapped between the two calls.#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector nums {1, 2, 5, 6, 8};
std::vector otherNums {3, 4, 7};
std::vector<int> difference;
std::vector<int> symmetricDifference;
std::set_symmetric_difference(nums.begin(), nums.end(),
otherNums.begin(), otherNums.end(),
std::back_inserter(symmetricDifference));
for (auto n : symmetricDifference) {
std::cout << n << " ";
}
std::cout << '\n';
std::set_difference(nums.begin(), nums.end(),
otherNums.begin(), otherNums.end(),
std::back_inserter(difference));
std::set_difference(otherNums.begin(), otherNums.end(),
nums.begin(), nums.end(),
std::back_inserter(difference));
for (auto n : difference) {
std::cout << n << " ";
}
std::cout << '\n';
}
/*
1 2 3 4 5 6 7 8
1 2 5 6 8 3 4 7
*/
set_symmetric_difference
and calling set_difference
- as you can see above - is that set_symmetric_difference
will output a sorted range while calling set_difference
twice will leave us with a container that has two sorted parts (the result of each call), but not sorted overall.set_symmetric_difference
is optimal for its purpose, unlike calling set_difference
twice.set_union
. This algorithm takes two ranges and will build another out of the elements that are present in either one or the other container.set_union
behaves like the previous ones. It takes two pairs of iterators as input, an output iterator an optional execution policy and a comparator.#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector nums {1, 1, 2, 2, 5, 6, 8};
std::vector otherNums {2, 5, 5, 7};
std::vector<int> unionOfNums;
std::set_union(nums.begin(), nums.end(),
otherNums.begin(), otherNums.end(),
std::back_inserter(unionOfNums));
for (auto n : unionOfNums) {
std::cout << n << " ";
}
std::cout << '\n';
}
/*
1 1 2 2 5 5 6 7 8
*/
2
, appears twice in the first input and once in the second. So it's taken twice from the first, and there is no excess in the second, so we are done.5
appears once in the first, so it's taken once from there and then there is one more item in the second input (2-1==1), so one more is taken there.32