From dbd2a89e755ac4941f3afbf3bf78ecb810d1bf55 Mon Sep 17 00:00:00 2001 From: Desislava Shunina <115353472+desiish@users.noreply.github.com> Date: Tue, 22 Oct 2024 17:37:35 +0300 Subject: Add pract03 readme, merge and partition implementations --- Practicum/pract03/partition.cpp | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Practicum/pract03/partition.cpp (limited to 'Practicum/pract03/partition.cpp') diff --git a/Practicum/pract03/partition.cpp b/Practicum/pract03/partition.cpp new file mode 100644 index 0000000..dd69876 --- /dev/null +++ b/Practicum/pract03/partition.cpp @@ -0,0 +1,42 @@ +#include +#include + +bool isEven(int n) { + return !(n % 2); +} + +template +RandomIt partition(RandomIt first, RandomIt last, Predicate p) +{ + RandomIt beg = first; + for (; beg != last; ++beg) + { + if (!p(*beg)) + break; + } + if (beg == last) + return beg; + + for (RandomIt it = std::next(beg); it != last; ++it) + { + if (p(*it)) + { + std::swap(*it, *beg); + ++beg; + } + } + + return beg; +} + +int main() +{ + std::vector v; + for (int i = 1; i <= 10; i++) + v.push_back(i); + + partition(v.begin(), v.end(), isEven); + + for (int i = 0; i < 10; i++) + std::cout << v[i] << " "; +} -- cgit v1.3.1-23-gd82d