Skip to content

Instantly share code, notes, and snippets.

@hidva
Last active August 1, 2016 07:13
Show Gist options
  • Save hidva/bd14d759d312fe577f0f8e5b4e7acdd2 to your computer and use it in GitHub Desktop.
Save hidva/bd14d759d312fe577f0f8e5b4e7acdd2 to your computer and use it in GitHub Desktop.
C++ 求取交集个数
struct MeteringIterator: public std::iterator<std::output_iterator_tag, void, void, void, void> {
public:
MeteringIterator(size_t *times_ptr) noexcept :
times(times_ptr) {
*times = 0;
}
template <typename T>
MeteringIterator& operator= (T &&elem) noexcept {
++*times;
return *this;
}
MeteringIterator& operator* () noexcept {
return *this;
}
MeteringIterator& operator++ () noexcept {
return *this;
}
MeteringIterator operator++ (int) noexcept {
return *this;
}
public:
size_t *times = nullptr;
};
template< class InputIt1, class InputIt2>
inline size_t GetSetIntersectionNumber(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) {
size_t times = 0;
std::set_intersection(first1, last1, first2, last2, MeteringIterator{&times});
return times;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment