📄 mutating_algo.h
字号:
template <typename Iterator, typename Predicate, typename T> class MPTLReplaceIf { private: Iterator first1, last1; Predicate pred; const T& newValue; public: typedef Iterator iterator1; typedef TwoIterators nIterators; MPTLReplaceIf(Iterator first1_, Iterator last1_, Predicate pred_, const T& newValue_) : first1(first1_), last1(last1_), pred(pred_), newValue(newValue_) { } Iterator getFirst1() { return first1; } Iterator getLast1() { return last1; } void applyAlgorithm(Iterator first, Iterator last) { std::replace_if(first, last, pred, newValue); } }; template <typename Iterator, typename Predicate, typename T> MPTLReplaceIf<Iterator, Predicate, T> replace_if(Iterator first, Iterator last, Predicate pred, const T& newValue) { return MPTLReplaceIf<Iterator, Predicate, T>(first, last, pred, newValue); } template <typename Iterator1, typename Iterator2, typename T> class MPTLReplaceCopy { private: Iterator1 first1, last1; Iterator2 first2; const T& oldValue; const T& newValue; public: typedef Iterator1 iterator1; typedef Iterator2 iterator2; typedef Iterator2 resultType; typedef LastElementPolicy reduction; typedef ThreeIterators nIterators; MPTLReplaceCopy(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_, const T& oldValue_, const T& newValue_) : first1(first1_), last1(last1_), first2(first2_), oldValue(oldValue_), newValue(newValue_) { } Iterator1 getFirst1() { return first1; } Iterator1 getLast1() { return last1; } Iterator2 getFirst2() { return first2; } resultType applyAlgorithm(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_) { return std::replace_copy(first1_, last1_, first2_, oldValue, newValue); } }; template <typename Iterator1, typename Iterator2, typename T> MPTLReplaceCopy<Iterator1, Iterator2, T> replace_copy(Iterator1 first, Iterator1 last, Iterator2 result, const T& oldValue, const T& newValue) { return MPTLReplaceCopy<Iterator1, Iterator2, T>(first, last, result, oldValue, newValue); } template <typename Iterator1, typename Iterator2, typename Predicate, typename T> class MPTLReplaceCopyIf { private: Iterator1 first1, last1; Iterator2 first2; Predicate pred; const T& newValue; public: typedef Iterator1 iterator1; typedef Iterator2 iterator2; typedef Iterator2 resultType; typedef LastElementPolicy reduction; typedef ThreeIterators nIterators; MPTLReplaceCopyIf(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_, Predicate pred_, const T& newValue_) : first1(first1_), last1(last1_), first2(first2_), pred(pred_), newValue(newValue_) { } Iterator1 getFirst1() { return first1; } Iterator1 getLast1() { return last1; } Iterator2 getFirst2() { return first2; } resultType applyAlgorithm(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_) { return std::replace_copy_if(first1_, last1_, first2_, pred, newValue); } }; template <typename Iterator1, typename Iterator2, typename Predicate, typename T> MPTLReplaceCopyIf<Iterator1, Iterator2, Predicate, T> replace_copy_if(Iterator1 first, Iterator1 last, Iterator2 result, Predicate pred, const T& newValue) { return MPTLReplaceCopyIf<Iterator1, Iterator2, Predicate, T>(first, last, result, pred, newValue); } template <typename Iterator, typename T> class MPTLFill { private: Iterator first1, last1; const T& value; public: typedef Iterator iterator1; typedef TwoIterators nIterators; MPTLFill(Iterator first1_, Iterator last1_, const T& value_) : first1(first1_), last1(last1_), value(value_) { } Iterator getFirst1() { return first1; } Iterator getLast1() { return last1; } void applyAlgorithm(Iterator first, Iterator last) { std::fill(first, last, value); } }; template <typename Iterator, typename T> MPTLFill<Iterator, T> fill(Iterator first, Iterator last, const T& value) { return MPTLFill<Iterator, T>(first, last, value); } template <typename Iterator, typename Size, typename T> class MPTLFillN { private: Iterator first1; const T& value; Iterator last1; public: typedef Iterator iterator1; typedef Iterator resultType; typedef LastElementPolicy reduction; typedef TwoIterators nIterators; MPTLFillN(Iterator first1_, Size n_, const T& value_) : first1(first1_), value(value_) { last1 = first1; std::advance(last1, n_); } Iterator getFirst1() { return first1; } Iterator getLast1() { return last1; } resultType applyAlgorithm(Iterator first, Iterator last) { return std::fill_n(first, std::distance(first, last), value); } }; template <typename Iterator, typename Size, typename T> MPTLFillN<Iterator, Size, T> fill_n(Iterator first, Size n, const T& value) { return MPTLFillN<Iterator, Size, T>(first, n, value); } template <typename Iterator, typename Generator> class MPTLGenerate { private: Iterator first1, last1; Generator gen; public: typedef Iterator iterator1; typedef TwoIterators nIterators; MPTLGenerate(Iterator first1_, Iterator last1_, Generator gen_) : first1(first1_), last1(last1_), gen(gen_) { } Iterator getFirst1() { return first1; } Iterator getLast1() { return last1; } void applyAlgorithm(Iterator first, Iterator last) { std::generate(first, last, gen); } }; template <typename Iterator, typename Generator> MPTLGenerate<Iterator, Generator> generate(Iterator first, Iterator last, Generator gen) { return MPTLGenerate<Iterator, Generator>(first, last, gen); } template <typename Iterator, typename Size, typename Generator> class MPTLGenerateN { private: Iterator first1; Generator gen; Iterator last1; public: typedef Iterator iterator1; typedef Iterator resultType; typedef LastElementPolicy reduction; typedef TwoIterators nIterators; MPTLGenerateN(Iterator first1_, Size n_, Generator gen_) : first1(first1_), gen(gen_) { last1 = first1; std::advance(last1, n_); } Iterator getFirst1() { return first1; } Iterator getLast1() { return last1; } resultType applyAlgorithm(Iterator first, Iterator last) { return std::generate_n(first, std::distance(first, last), gen); } }; template <typename Iterator, typename Size, typename Generator> MPTLGenerateN<Iterator, Size, Generator> generate_n(Iterator first, Size n, Generator gen) { return MPTLGenerateN<Iterator, Size, Generator>(first, n, gen); } } // namespace mptl#endif /* _MUTATING_ALGO */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -