STL-Lesson004_002
来源:互联网 发布:python绘图 编辑:程序博客网 时间:2024/06/18 08:26
#ifndef _MYALGORITHM_H_#define _MYALGORITHM_H_#include <iterator>#include <random>#include <type_traits>#include <tuple>namespace PoEdu{ // all_of // Test condition on all elements in range (function template ) template<typename InputIterator, typename UnaryPredicate> bool all_of(InputIterator first, InputIterator last, UnaryPredicate pred) { bool ret = true; while (first != last) { if (!pred(*first)) { ret = false; break; } ++first; } return ret; } // any_of // Test if any element in range fulfills condition (function template ) template<typename InputIterator, typename UnaryPredicate> bool any_of(InputIterator first, InputIterator last, UnaryPredicate pred) { bool ret = false; while (first != last) { if (pred(*first)) { ret = true; break; } ++first; } return ret; } // none_of // Test if no elements fulfill condition (function template ) template<typename InputIterator, typename UnaryPredicate> bool none_of(InputIterator first, InputIterator last, UnaryPredicate pred) { bool ret = true; while (first != last) { if (pred(*first)) { ret = false; break; } ++first; } return ret; } // for_each // Apply function to range (function template ) template<typename InputIterator, typename Function> Function for_each(InputIterator first, InputIterator last, Function fn) { while (first != last) { fn(*first); ++first; } return fn; } // find // Find value in range (function template ) template<typename InputIterator, typename T> InputIterator find(InputIterator first, InputIterator last, const T& val) { InputIterator ret = last; while (first != last) { if (*first == val) { ret = first; break; } ++first; } return ret; } // find_if // Find element in range (function template ) template<typename InputIterator, typename UnaryPredicate> InputIterator find_if(InputIterator first, InputIterator last, UnaryPredicate pred) { InputIterator ret = last; while (first != last) { if (pred(*first)) { ret = first; break; } ++first; } return ret; } // find_if_not // Find element in range(negative condition) (function template) template<typename InputIterator, typename UnaryPredicate> InputIterator find_if_not(InputIterator first, InputIterator last, UnaryPredicate pred) { InputIterator ret = last; while (first != last) { if (!pred(*first)) { ret = first; break; } ++first; } return ret; } // find_end // Find last subsequence in range(function template) template<typename ForwardIterator1, typename ForwardIterator2> ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) { if (first2 == last2) { return last1; } ForwardIterator1 ret = last1; while (first1 != last1) { ForwardIterator1 it1 = first1; ForwardIterator2 it2 = first2; while (*it1 == *it2) { ++it1; ++it2; if (it2 == last2) { ret = first1; break; } if (it1 == last1) { return ret; } } ++first1; } return ret; } template <typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate> ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred) { if (first2 == last2) { return last1; } ForwardIterator1 ret = last1; while (first1 != last1) { ForwardIterator1 it1 = first1; ForwardIterator2 it2 = first2; while (pred(*it1, *it2)) { ++it1; ++it2; if (it2 == last2) { ret = first1; break; } if (it1 == last1) { return ret; } } ++first1; } return ret; } // find_first_of // Find element from set in range(function template) template<typename InputIterator, typename ForwardIterator> InputIterator find_first_of(InputIterator first1, InputIterator last1, ForwardIterator first2, ForwardIterator last2) { while (first1 != last1) { for (ForwardIterator it = first2; it != last2; ++it) { if (*it == *first1) { return first1; } } ++first1; } return last1; } template <typename InputIterator, typename ForwardIterator, typename BinaryPredicate> InputIterator find_first_of(InputIterator first1, InputIterator last1, ForwardIterator first2, ForwardIterator last2, BinaryPredicate pred) { while (first1 != last1) { for (ForwardIterator it = first2; it != last2; ++it) { if (pred(*it, *first1)) { return first1; } } ++first1; } return last1; } // adjacent_find // Find equal adjacent elements in range(function template) template <typename ForwardIterator> ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last) { ForwardIterator ret = last; if (first != last) { ForwardIterator next = first; ++next; while (next != last) { if (*first == *next) { ret = first; break; } ++first; ++next; } } return ret; } template <typename ForwardIterator, typename BinaryPredicate> ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred) { ForwardIterator ret = last; if (first != last) { ForwardIterator next = first; ++next; while (next != last) { if (pred(*first, *next)) { ret = first; break; } ++first; ++next; } } return ret; } // count // Count appearances of value in range(function template) template <typename InputIterator, typename T> typename std::iterator_traits<InputIterator>::difference_type count(InputIterator first, InputIterator last, const T& val) { typename std::iterator_traits<InputIterator>::difference_type ret = 0; while (first != last) { if (*first == val) { ++ret; } ++first; } return ret; } // count_if // Return number of elements in range satisfying condition(function template) template <typename InputIterator, typename UnaryPredicate> typename std::iterator_traits<InputIterator>::difference_type count_if(InputIterator first, InputIterator last, UnaryPredicate pred) { typename std::iterator_traits<InputIterator>::difference_type ret = 0; while (first != last) { if (pred(*first)) { ++ret; } ++first; } return ret; } // mismatch // Return first position where two ranges differ(function template) template <typename InputIterator1, typename InputIterator2> std::pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2) { while ((first1 != last1) && (*first1 == *first2)) { ++first1; ++first2; } return std::make_pair(first1, first2); } template <typename InputIterator1, typename InputIterator2, typename BinaryPredicate> std::pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred) { while ((first1 != last1) && (pred(*first1, *first2))) { ++first1; ++first2; } return std::make_pair(first1, first2); } // equal // Test whether the elements in two ranges are equal(function template) template <typename InputIterator1, typename InputIterator2> bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2) { bool ret = true; while (first1 != last1) { if (!(*first1 == *first2)) { ret = false; } ++first1; ++first2; } return ret; } template <typename InputIterator1, typename InputIterator2, typename BinaryPredicate> bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred) { bool ret = true; while (first1 != last1) { if (!(pred(*first1, *first2))) { ret = false; } ++first1; ++first2; } return ret; } // is_permutation // Test whether range is permutation of another(function template) template <typename InputIterator1, typename InputIterator2> bool is_permutation(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2) { std::tie(first1, first2) = PoEdu::mismatch(first1, last1, first2); if (first1 == last1) return true; InputIterator2 last2 = first2; std::advance(last2, std::distance(first1, last1)); for (InputIterator1 it1 = first1; it1 != last1; ++it1) { if (PoEdu::find(first1, it1, *it1) == it1) { auto n = PoEdu::count(first2, last2, *it1); if (n == 0 || PoEdu::count(it1, last1, *it1) != n) return false; } } return true; } template <typename InputIterator1, typename InputIterator2, typename BinaryPredicate> bool is_permutation(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred) { std::tie(first1, first2) = PoEdu::mismatch(first1, last1, first2); if (first1 == last1) { return true; } InputIterator2 last2 = first2; std::advance(last2, std::distance(first1, last1)); for (InputIterator1 it1 = first1; it1 != last1; ++it1) { if (find(first1, it1, *it1) == it1) { auto n = count(first2, last2, *it1); if (n == 0 || count(it1, last1, *it1) != n) { return false; } } } return true; } // search // Search range for subsequence(function template) template<typename ForwardIterator1, typename ForwardIterator2> ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) { if (first2 == last2) { return first1; } while (first1 != last1) { ForwardIterator1 it1 = first1; ForwardIterator2 it2 = first2; while (*it1 == *it2) { ++it1; ++it2; if (it2 == last2) { return first1; } if (it1 == last1) { return last1; } } ++first1; } return last1; } template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred) { if (first2 == last2) { return first1; } while (first1 != last1) { ForwardIterator1 it1 = first1; ForwardIterator2 it2 = first2; while (pred(*it1, *it2)) { ++it1; ++it2; if (it2 == last2) { return first1; } if (it1 == last1) { return last1; } } ++first1; } return last1; } // search_n // Search range for elements(function template) template<typename ForwardIterator, typename Size, typename T> ForwardIterator search_n(ForwardIterator first, ForwardIterator last, Size count, const T& val) { ForwardIterator it, limit; Size i; limit = first; std::advance(limit, std::distance(first, last) - count); while (first != limit) { it = first; i = 0; while (*it == val) { ++it; if (++i == count) { return first; } } ++first; } return last; } template <typename ForwardIterator, typename Size, typename T, typename BinaryPredicate> ForwardIterator search_n(ForwardIterator first, ForwardIterator last, Size count, const T& val, BinaryPredicate pred) { ForwardIterator it, limit; Size i; limit = first; std::advance(limit, std::distance(first, last) - count); while (first != limit) { it = first; i = 0; while (pred(*it, val)) { ++it; if (++i == count) { return first; } } ++first; } return last; } // copy // Copy range of elements(function template) template<typename InputIterator, typename OutputIterator> OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result) { while (first != last) { *result = *first; ++result; ++first; } return result; } // copy_n // Copy elements(function template) template<typename InputIterator, typename Size, typename OutputIterator> OutputIterator copy_n(InputIterator first, Size n, OutputIterator result) { while (n > 0) { *result = *first; ++result; ++first; --n; } return result; } // copy_if // Copy certain elements of range(function template) template <typename InputIterator, typename OutputIterator, typename UnaryPredicate> OutputIterator copy_if(InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate pred) { while (first != last) { if (pred(*first)) { *result = *first; ++result; } ++first; } return result; } // copy_backward // Copy range of elements backward(function template) template<typename BidirectionalIterator1, typename BidirectionalIterator2> BidirectionalIterator2 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result) { while (last != first) *(--result) = *(--last); return result; } // move // Move range of elements(function template) template<typename InputIterator, typename OutputIterator> OutputIterator move(InputIterator first, InputIterator last, OutputIterator result) { while (first != last) { *result = std::move(*first); ++result; ++first; } return result; } // move_backward // Move range of elements backward(function template) template<typename BidirectionalIterator1, typename BidirectionalIterator2> BidirectionalIterator2 move_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result) { while (last != first) { *(--result) = PoEdu::move(*(--last)); } return result; } // swap // Exchange values of two objects(function template) template <typename T> void swap(T &a, T &b) { T c(std::move(a)); a = std::move(b); b = std::move(c); } template <typename T, size_t N> void swap(T(&a)[N], T(&b)[N]) { for (size_t i = 0; i < N; ++i) { PoEdu::swap(a[i], b[i]); } } // swap_ranges // Exchange values of two ranges(function template) template<typename ForwardIterator1, typename ForwardIterator2> ForwardIterator2 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2) { while (first1 != last1) { PoEdu::swap(*first1, *first2); ++first1; ++first2; } return first2; } // iter_swap // Exchange values of objects pointed to by two iterators(function template) template <typename ForwardIterator1, typename ForwardIterator2> void iter_swap(ForwardIterator1 a, ForwardIterator2 b) { PoEdu::swap(*a, *b); } // transform // Transform range(function template) template <typename InputIterator, typename OutputIterator, typename UnaryOperator> OutputIterator transform(InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperator op) { while (first1 != last1) { *result = op(*first1); ++result; ++first1; } return result; } template <typename InputIterator1, typename InputIterator2, typename OutputIterator, typename BinaryOperation> OutputIterator transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryOperation binary_op) { while (first1 != last1) { *result = binary_op(*first1, *first2); ++result; ++first1; ++first2; } return result; } // replace // Replace value in range(function template) template <typename ForwardIterator, typename T> void replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value) { while (first != last) { if (*first == old_value) { *first = new_value; } ++first; } } // replace_if // Replace values in range(function template) template < typename ForwardIterator, typename UnaryPredicate, typename T> void replace_if(ForwardIterator first, ForwardIterator last, UnaryPredicate pred, const T& new_value) { while (first != last) { if (pred(*first)) { *first = new_value; } ++first; } } // replace_copy // Copy range replacing value(function template) template <typename InputIterator, typename OutputIterator, typename T> OutputIterator replace_copy(InputIterator first, InputIterator last, OutputIterator result, const T& old_value, const T& new_value) { while (first != last) { *result = (*first == old_value) ? new_value : *first; ++first; ++result; } return result; } // replace_copy_if // Copy range replacing value(function template) template <typename InputIterator, typename OutputIterator, typename UnaryPredicate, typename T> OutputIterator replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate pred, const T& new_value) { while (first != last) { *result = (pred(*first)) ? new_value : *first; ++first; ++result; } return result; } // fill // Fill range with value(function template) template <typename ForwardIterator, typename T> void fill(ForwardIterator first, ForwardIterator last, const T& val) { while (first != last) { *first = val; ++first; } } // fill_n // Fill sequence with value(function template) template <typename OutputIterator, typename Size, typename T> OutputIterator fill_n(OutputIterator first, Size n, const T& val) { while (n > 0) { *first = val; ++first; --n; } return first; } // generate // Generate values for range with function(function template) template <typename ForwardIterator, typename Generator> void generate(ForwardIterator first, ForwardIterator last, Generator gen) { while (first != last) { *first = gen(); ++first; } } // generate_n // Generate values for sequence with function(function template) template <typename OutputIterator, typename Size, typename Generator> void generate_n(OutputIterator first, Size n, Generator gen) { while (n > 0) { *first = gen(); ++first; --n; } } // remove // Remove value from range(function template) template <typename ForwardIterator, typename T> ForwardIterator remove(ForwardIterator first, ForwardIterator last, const T& val) { ForwardIterator result = first; while (first != last) { if (!(*first == val)) { *result = std::move(*first); ++result; } ++first; } return result; } // remove_if // Remove elements from range(function template) template <typename ForwardIterator, typename UnaryPredicate> ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, UnaryPredicate pred) { ForwardIterator result = first; while (first != last) { if (!pred(*first)) { *result = std::move(*first); ++result; } ++first; } return result; } // remove_copy // Copy range removing value(function template) template <typename InputIterator, typename OutputIterator, typename T> OutputIterator remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& val) { while (first != last) { if (!(*first == val)) { *result = *first; ++result; } ++first; } return result; } // remove_copy_if // Copy range removing values(function template) template <typename InputIterator, typename OutputIterator, typename UnaryPredicate> OutputIterator remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate pred) { while (first != last) { if (!pred(*first)) { *result = *first; ++result; } ++first; } return result; } // unique // Remove consecutive duplicates in range(function template) template <typename ForwardIterator> ForwardIterator unique(ForwardIterator first, ForwardIterator last) { if (first == last) { return last; } ForwardIterator result = first; while (++first != last) { if (!(*result == *first)) { *(++result) = *first; } } return ++result; } template <typename ForwardIterator, typename BinaryPredicate> ForwardIterator unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred) { if (first == last) { return last; } ForwardIterator result = first; while (++first != last) { if (!(pred(*result, *first))) { *(++result) = *first; } } return ++result; } // unique_copy // Copy range removing duplicates(function template) template <typename InputIterator, typename OutputIterator> OutputIterator unique_copy(InputIterator first, InputIterator last, OutputIterator result) { if (first == last) { return result; } *result = *first; while (++first != last) { typename std::iterator_traits<InputIterator>::value_type val = *first; if (!(*result == val)) { *(++result) = val; } } return ++result; } template <typename InputIterator, typename OutputIterator, typename BinaryPredicate> OutputIterator unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred) { if (first == last) { return result; } *result = *first; while (++first != last) { typename std::iterator_traits<InputIterator>::value_type val = *first; if (!(pred(*result, val))) { *(++result) = val; } } return ++result; } // reverse // Reverse range(function template) template <typename BidirectionalIterator> void reverse(BidirectionalIterator first, BidirectionalIterator last) { while ((first != last) && (first != --last)) { PoEdu::iter_swap(first, last); ++first; } } // reverse_copy // Copy range reversed(function template) template <typename BidirectionalIterator, typename OutputIterator> OutputIterator reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result) { while (first != last) { --last; *result = *last; ++result; } return result; } // rotate // Rotate left the elements in range(function template) template <typename ForwardIterator> void rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last) { ForwardIterator next = middle; while (first != next) { PoEdu::swap(*first++, *next++); if (next == last) { next = middle; } else if (first == middle) { middle = next; } } } // rotate_copy // Copy range rotated left(function template) template <typename ForwardIterator, typename OutputIterator> OutputIterator rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result) { result = PoEdu::copy(middle, last, result); return PoEdu::copy(first, middle, result); } // random_shuffle // Randomly rearrange elements in range(function template) template <typename RandomAccessIterator> void random_shuffle(RandomAccessIterator first, RandomAccessIterator last) { PoEdu::random_shuffle(first, last, [](int i) { return rand() % i; }); } template <typename RandomAccessIterator, typename RandomNumberGenerator> void random_shuffle(RandomAccessIterator first, RandomAccessIterator last, RandomNumberGenerator& gen) { std::iterator_traits<RandomAccessIterator>::difference_type i, n; n = (last - first); for (i = n - 1; i > 0; --i) { PoEdu::swap(first[i], first[gen(i + 1)]); //PoEdu::swap(first[i], first[1+i]); } } // shuffle // Randomly rearrange elements in range using generator(function template) template <typename RandomAccessIterator, typename URNG> void shuffle(RandomAccessIterator first, RandomAccessIterator last, URNG&& g) { for (auto i = (last - first) - 1; i > 0; --i) { std::uniform_int_distribution<decltype(i)> d(0, i); PoEdu::swap(first[i], first[d(g)]); } }}#endif//!_MYALGORITHM_H_
Test
#include <iostream>#include <vector>#include <ctime>#include <cstdlib> #include "MyAlgorithm.h"#define _TEST_COPY_//#define _TEST_COPY_N_//#define _TEST_COPY_IF_//#define _TEST_COPY_BACKWARD_//#define _TEST_SWAP_//#define _TEST_SWAP_RANGES_//#define _TEST_ITER_SWAP_//#define _TEST_TRANSFORM_//#define _TEST_REPLACE_//#define _TEST_REPLACE_IF_//#define _TEST_REPLACE_COPY_//#define _TEST_REPLACE_COPY_IF_//#define _TEST_FILL_//#define _TEST_FILL_N_//#define _TEST_GENERATE_//#define _TEST_GENERATE_N_//#define _TEST_REMOVE_//#define _TEST_REMOVE_IF_//#define _TEST_REMOVE_COPY_//#define _TEST_REMOVE_COPY_IF_//#define _TEST_UNIQUE_//#define _TEST_UNIQUE_COPY_//#define _TEST_REVERSE_//#define _TEST_REVERSE_COPY_//#define _TEST_ROTATE_//#define _TEST_ROTATE_COPY_//#define _TEST_RANDOM_SHANFFLE_//#define _TEST_RANDOM_#ifdef _TEST_TRANSFORM_int op_increase(int i){ return ++i;}#endif//!_TEST_TRANSFORM_#ifdef _TEST_REPLACE_IF_bool IsOdd(int i){ return ((i % 2) == 1);}#endif//!_TEST_REPLACE_IF_#ifdef _TEST_REPLACE_COPY_IF_bool IsOdd(int i){ return ((i % 2) == 1);}#endif//!_TEST_REPLACE_COPY_IF_#ifdef _TEST_GENERATE_// function generator:int RandomNumber(){ return (std::rand() % 100);}// class generator:struct c_unique{ int current; c_unique() { current = 0; } int operator()() { return ++current; }} UniqueNumber;#endif//!_TEST_GENERATE_#ifdef _TEST_GENERATE_N_int current = 0;int UniqueNumber(){ return ++current;}#endif//!_TEST_GENERATE_N_#ifdef _TEST_REMOVE_IF_bool IsOdd(int i){ return ((i % 2) == 1);}#endif//!_TEST_REMOVE_IF_#ifdef _TEST_REMOVE_COPY_IF_bool IsOdd(int i){ return ((i % 2) == 1);}#endif//!_TEST_REMOVE_COPY_IF_#ifdef _TEST_UNIQUE_bool myfunction(int i, int j){ return (i == j);}#endif//!_TEST_UNIQUE_#ifdef _TEST_UNIQUE_COPY_#include <algorithm>bool myfunction(int i, int j){ return (i == j);}#endif//!_TEST_UNIQUE_COPY_#ifdef _TEST_RANDOM_SHANFFLE_#include <ctime>#include <cstdlib>int myrandom(int i){ return std::rand() % i;}#endif//!_TEST_RANDOM_SHANFFLE_#ifdef _TEST_RANDOM_#include <chrono>#include <array>#endif//!_TEST_RANDOM_int main(){#ifdef _TEST_COPY_ std::cout << "test copy..." << std::endl; int myints[] = { 10,20,30,40,50,60,70 }; std::vector<int> myvector(7); PoEdu::copy(myints, myints + 7, myvector.begin()); std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_COPY_#ifdef _TEST_COPY_N_ std::cout << "test copy_n..." << std::endl; int myints[] = { 10,20,30,40,50,60,70 }; std::vector<int> myvector; myvector.resize(7); // allocate space for 7 elements PoEdu::copy_n(myints, 7, myvector.begin()); std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_COPY_N_#ifdef _TEST_COPY_IF_ std::cout << "test copy_if..." << std::endl; std::vector<int> foo = { 25,15,5,-5,-15 }; std::vector<int> bar(foo.size()); // copy only positive numbers: auto it = PoEdu::copy_if(foo.begin(), foo.end(), bar.begin(), [](int i) {return !(i < 0); }); bar.resize(std::distance(bar.begin(), it)); // shrink container to new size std::cout << "bar contains:"; for (int& x : bar) std::cout << ' ' << x; std::cout << '\n';#endif//!_TEST_COPY_IF_#ifdef _TEST_COPY_BACKWARD_ std::cout << "test copy_backward..." << std::endl; std::vector<int> myvector; // set some values: for (int i = 1; i <= 5; i++) { myvector.push_back(i * 10); // myvector: 10 20 30 40 50 } myvector.resize(myvector.size() + 3); // allocate space for 3 more elements PoEdu::copy_backward(myvector.begin(), myvector.begin() + 5, myvector.end()); std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_COPY_BACKWARD_#ifdef _TEST_SWAP_ std::cout << "test swap..." << std::endl; int x = 10, y = 20; // x:10 y:20 PoEdu::swap(x, y); // x:20 y:10 std::vector<int> foo(4, x), bar(6, y); // foo:4x20 bar:6x10 PoEdu::swap(foo, bar); // foo:6x10 bar:4x20 std::cout << "foo contains:"; for (std::vector<int>::iterator it = foo.begin(); it != foo.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_SWAP_#ifdef _TEST_SWAP_RANGES_ std::cout << "test swap_ranges..." << std::endl; std::vector<int> foo(5, 10); // foo: 10 10 10 10 10 std::vector<int> bar(5, 33); // bar: 33 33 33 33 33 PoEdu::swap_ranges(foo.begin() + 1, foo.end() - 1, bar.begin()); // print out results of swap: std::cout << "foo contains:"; for (std::vector<int>::iterator it = foo.begin(); it != foo.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n'; std::cout << "bar contains:"; for (std::vector<int>::iterator it = bar.begin(); it != bar.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_SWAP_RANGES_#ifdef _TEST_ITER_SWAP_ std::cout << "test iter_swap..." << std::endl; int myints[] = { 10,20,30,40,50 }; // myints: 10 20 30 40 50 std::vector<int> myvector(4, 99); // myvector: 99 99 99 99 PoEdu::iter_swap(myints, myvector.begin()); // myints: [99] 20 30 40 50 // myvector: [10] 99 99 99 PoEdu::iter_swap(myints + 3, myvector.begin() + 2); // myints: 99 20 30 [99] 50 // myvector: 10 99 [40] 99 std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_ITER_SWAP_#ifdef _TEST_TRANSFORM_ std::cout << "test transform..." << std::endl; std::vector<int> foo; std::vector<int> bar; // set some values: for (int i = 1; i < 6; i++) { foo.push_back(i * 10); // foo: 10 20 30 40 50 } bar.resize(foo.size()); // allocate space PoEdu::transform(foo.begin(), foo.end(), bar.begin(), op_increase); // bar: 11 21 31 41 51 // std::plus adds together its two arguments: PoEdu::transform(foo.begin(), foo.end(), bar.begin(), foo.begin(), std::plus<int>()); // foo: 21 41 61 81 101 std::cout << "foo contains:"; for (std::vector<int>::iterator it = foo.begin(); it != foo.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_TRANSFORM_#ifdef _TEST_REPLACE_ std::cout << "test replace..." << std::endl; int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 }; std::vector<int> myvector(myints, myints + 8); // 10 20 30 30 20 10 10 20 PoEdu::replace(myvector.begin(), myvector.end(), 20, 99); // 10 99 30 30 99 10 10 99 std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_REPLACE_#ifdef _TEST_REPLACE_IF_ std::cout << "test replace_if..." << std::endl; std::vector<int> myvector; // set some values: for (int i = 1; i < 10; i++) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 PoEdu::replace_if(myvector.begin(), myvector.end(), IsOdd, 0); // 0 2 0 4 0 6 0 8 0 std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_REPLACE_IF_#ifdef _TEST_REPLACE_COPY_ std::cout << "test replace_copy..." << std::endl; int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 }; std::vector<int> myvector(8); PoEdu::replace_copy(myints, myints + 8, myvector.begin(), 20, 99); std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_REPLACE_COPY_#ifdef _TEST_REPLACE_COPY_IF_ std::cout << "test replace_copy_if..." << std::endl; std::vector<int> foo, bar; // set some values: for (int i = 1; i < 10; i++) { foo.push_back(i); // 1 2 3 4 5 6 7 8 9 } bar.resize(foo.size()); // allocate space PoEdu::replace_copy_if(foo.begin(), foo.end(), bar.begin(), IsOdd, 0); // 0 2 0 4 0 6 0 8 0 std::cout << "bar contains:"; for (std::vector<int>::iterator it = bar.begin(); it != bar.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_REPLACE_COPY_IF_#ifdef _TEST_FILL_ std::cout << "test fill..." << std::endl; std::vector<int> myvector(8); // myvector: 0 0 0 0 0 0 0 0 PoEdu::fill(myvector.begin(), myvector.begin() + 4, 5); // myvector: 5 5 5 5 0 0 0 0 PoEdu::fill(myvector.begin() + 3, myvector.end() - 2, 8); // myvector: 5 5 5 8 8 8 0 0 std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_FILL_#ifdef _TEST_FILL_N_ std::cout << "test fill_n..." << std::endl; std::vector<int> myvector(8, 10); // myvector: 10 10 10 10 10 10 10 10 PoEdu::fill_n(myvector.begin(), 4, 20); // myvector: 20 20 20 20 10 10 10 10 PoEdu::fill_n(myvector.begin() + 3, 3, 33); // myvector: 20 20 20 33 33 33 10 10 std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_FILL_N_#ifdef _TEST_GENERATE_ std::cout << "test generate..." << std::endl; std::srand(unsigned(std::time(0))); std::vector<int> myvector(8); PoEdu::generate(myvector.begin(), myvector.end(), RandomNumber); std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n'; PoEdu::generate(myvector.begin(), myvector.end(), UniqueNumber); std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_GENERATE_#ifdef _TEST_GENERATE_N_ std::cout << "test generate_n..." << std::endl; int myarray[9]; PoEdu::generate_n(myarray, 9, UniqueNumber); std::cout << "myarray contains:"; for (int i = 0; i < 9; ++i) { std::cout << ' ' << myarray[i]; } std::cout << '\n';#endif//!_TEST_GENERATE_N_#ifdef _TEST_REMOVE_ std::cout << "test remove..." << std::endl; int myints[] = { 10,20,30,30,20,10,10,20 }; // 10 20 30 30 20 10 10 20 // bounds of range: int* pbegin = myints; // ^ int* pend = myints + sizeof(myints) / sizeof(int); // ^ ^ pend = PoEdu::remove(pbegin, pend, 20); // 10 30 30 10 10 ? ? ? // ^ ^ std::cout << "range contains:"; for (int* p = pbegin; p != pend; ++p) { std::cout << ' ' << *p; } std::cout << '\n';#endif//!_TEST_REMOVE_#ifdef _TEST_REMOVE_IF_ std::cout << "test remove_if..." << std::endl; int myints[] = { 1,2,3,4,5,6,7,8,9 }; // 1 2 3 4 5 6 7 8 9 // bounds of range: int* pbegin = myints; // ^ int* pend = myints + sizeof(myints) / sizeof(int); // ^ ^ pend = PoEdu::remove_if(pbegin, pend, IsOdd); // 2 4 6 8 ? ? ? ? ? // ^ ^ std::cout << "the range contains:"; for (int* p = pbegin; p != pend; ++p) { std::cout << ' ' << *p; } std::cout << '\n';#endif//!_TEST_REMOVE_IF_#ifdef _TEST_REMOVE_COPY_ std::cout << "test remove_copy..." << std::endl; int myints[] = { 10,20,30,30,20,10,10,20 }; // 10 20 30 30 20 10 10 20 std::vector<int> myvector(8); PoEdu::remove_copy(myints, myints + 8, myvector.begin(), 20); // 10 30 30 10 10 0 0 0 std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_REMOVE_COPY_#ifdef _TEST_REMOVE_COPY_IF_ std::cout << "test remove_copy_if..." << std::endl; int myints[] = { 1,2,3,4,5,6,7,8,9 }; std::vector<int> myvector(9); PoEdu::remove_copy_if(myints, myints + 9, myvector.begin(), IsOdd); std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_REMOVE_COPY_IF_#ifdef _TEST_UNIQUE_ std::cout << "test unique..." << std::endl; int myints[] = { 10,20,20,20,30,30,20,20,10 }; // 10 20 20 20 30 30 20 20 10 std::vector<int> myvector(myints, myints + 9); // using default comparison: std::vector<int>::iterator it; it = PoEdu::unique(myvector.begin(), myvector.end()); // 10 20 30 20 10 ? ? ? ? // ^ myvector.resize(std::distance(myvector.begin(), it)); // 10 20 30 20 10 // using predicate comparison: PoEdu::unique(myvector.begin(), myvector.end(), myfunction); // (no changes) // print out content: std::cout << "myvector contains:"; for (it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_UNIQUE_#ifdef _TEST_UNIQUE_COPY_ std::cout << "test unique_copy..." << std::endl; int myints[] = { 10,20,20,20,30,30,20,20,10 }; std::vector<int> myvector(9); // 0 0 0 0 0 0 0 0 0 // using default comparison: std::vector<int>::iterator it; it = PoEdu::unique_copy(myints, myints + 9, myvector.begin()); // 10 20 30 20 10 0 0 0 0 // ^ std::sort(myvector.begin(), it); // 10 10 20 20 30 0 0 0 0 // ^ // using predicate comparison: it = PoEdu::unique_copy(myvector.begin(), it, myvector.begin(), myfunction); // 10 20 30 20 30 0 0 0 0 // ^ myvector.resize(std::distance(myvector.begin(), it)); // 10 20 30 // print out content: std::cout << "myvector contains:"; for (it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_UNIQUE_COPY_#ifdef _TEST_REVERSE_ std::cout << "test reverse..." << std::endl; std::vector<int> myvector; // set some values: for (int i = 1; i < 10; ++i) { myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 } PoEdu::reverse(myvector.begin(), myvector.end()); // 9 8 7 6 5 4 3 2 1 // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_REVERSE_#ifdef _TEST_REVERSE_COPY_ std::cout << "test reverse_copy..." << std::endl; int myints[] = { 1,2,3,4,5,6,7,8,9 }; std::vector<int> myvector; myvector.resize(9); // allocate space PoEdu::reverse_copy(myints, myints + 9, myvector.begin()); // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_REVERSE_COPY_#ifdef _TEST_ROTATE_ std::cout << "test rotate..." << std::endl; std::vector<int> myvector; // set some values: for (int i = 1; i < 10; ++i) { myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 } PoEdu::rotate(myvector.begin(), myvector.begin() + 3, myvector.end()); // 4 5 6 7 8 9 1 2 3 // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_ROTATE_#ifdef _TEST_ROTATE_COPY_ std::cout << "test rotate_copy..." << std::endl; int myints[] = { 10,20,30,40,50,60,70 }; std::vector<int> myvector(7); PoEdu::rotate_copy(myints, myints + 3, myints + 7, myvector.begin()); // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_ROTATE_COPY_#ifdef _TEST_RANDOM_SHANFFLE_ std::cout << "test random_shuffle..." << std::endl; std::srand(unsigned(std::time(0))); std::vector<int> myvector; // set some values: for (int i = 1; i < 10; ++i) { myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 } // using built-in random generator: PoEdu::random_shuffle(myvector.begin(), myvector.end()); // using myrandom: PoEdu::random_shuffle(myvector.begin(), myvector.end(), myrandom); // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << ' ' << *it; } std::cout << '\n';#endif//!_TEST_RANDOM_SHANFFLE_#ifdef _TEST_RANDOM_ std::cout << "test shuffle..." << std::endl; std::array<int, 5> foo{ 1,2,3,4,5 }; // obtain a time-based seed: long long seed = std::chrono::system_clock::now().time_since_epoch().count(); PoEdu::shuffle(foo.begin(), foo.end(), std::default_random_engine(seed)); std::cout << "shuffled elements:"; for (int& x : foo) { std::cout << ' ' << x; } std::cout << '\n';#endif//!_TEST_RANDOM_ return 0;}
阅读全文
0 0
- STL-Lesson004_002
- stl
- stl
- STL
- STL
- STL
- STL
- STL
- STL
- STL
- stl
- STL
- STL
- STL
- STL
- STL
- STL
- STL
- STL-Lesson003_学生管理系统
- Android litepal配置
- STL-Lesson004_001
- 打印菱形
- 软件设计方案
- STL-Lesson004_002
- 查看Nginx、apache、MySQL和PHP的编译参数
- keil5 STM32 浮点数转字符串跑飞
- 好接口的设计与实现
- keil STM32 hex转浮点数及浮点数转字符串较好方案
- STL_Lesson005_001
- Leetcode 题解系列(十二)
- 戴尔7447 win10安装Ubuntu双系统
- HQL补充知识点