Last active
August 1, 2016 07:13
-
-
Save hidva/bd14d759d312fe577f0f8e5b4e7acdd2 to your computer and use it in GitHub Desktop.
C++ 求取交集个数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{×}); | |
return times; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment