### is_partitioned_until Algorithm Examples Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/Misc Illustrates the use of the is_partitioned_until algorithm with different predicates and ranges. The examples show how it returns an iterator to the point where the partitioning condition is no longer met. ```cpp bool isOdd ( int i ) { return i % 2 == 1; } bool lessThan10 ( int i ) { return i < 10; } // Assuming 'c' is a container like std::vector c = { 0, 1, 2, 3, 14, 15 }; is_partitioned_until ( c, isOdd ) --> iterator to '1' is_partitioned_until ( c, lessThan10 ) --> end is_partitioned_until ( c.begin (), c.end (), lessThan10 ) --> end is_partitioned_until ( c.begin (), c.begin () + 3, lessThan10 ) --> end is_partitioned_until ( c.end (), c.end (), isOdd ) --> end // empty range ``` -------------------------------- ### apply_permutation and apply_reverse_permutation Examples (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/Misc Demonstrates the usage of apply_permutation and apply_reverse_permutation with various container sizes and order sequences. Illustrates how these functions modify the input sequences based on the provided permutations. ```cpp std::vector emp_vec, emp_order; std::vector one{1}, one_order{0}; std::vector two{1,2}, two_order{1,0}; std::vector vec{1, 2, 3, 4, 5}; std::vector order{4, 2, 3, 1, 0}; apply_permutation(emp_vec, emp_order); // no changes apply_reverse_permutation(emp_vec, emp_order); // no changes apply_permutation(one, one_order); // no changes apply_reverse_permutation(one, one_order); // no changes apply_permutation(two, two_order); // two:{2,1} apply_reverse_permutation(two, two_order); // two:{2,1} apply_permutation(vec, order); // vec:{5, 3, 4, 2, 1} apply_reverse_permutation(vec, order); // vec:{5, 4, 2, 3, 1} ``` -------------------------------- ### C++ equal Algorithm Usage Examples Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/CXX14 Illustrates practical examples of using the 'equal' algorithm with different container iterators and ranges. It demonstrates comparisons between non-equal sequences, sub-sequences, and the handling of empty sequences, which are always considered equal. ```cpp auto c1 = { 0, 1, 2, 3, 14, 15 }; auto c2 = { 1, 2, 3 }; equal ( c1.begin (), c1.end (), c2.begin (), c2.end()) --> false equal ( c1.begin () + 1, c1.begin () + 3, c2.begin (), c2.end()) --> true equal ( c1.end (), c1.end (), c2.end (), c2.end()) --> true // empty sequences are alway equal to each other ``` -------------------------------- ### partition_point - Usage examples with predicates Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/CXX11 Demonstrates partition_point usage with a container {0, 1, 2, 3, 14, 15} and predicates lessThan10 and isOdd. Shows both iterator and range-based interfaces returning positions of first elements not satisfying predicates. ```C++ bool lessThan10(int i) { return i < 10; } bool isOdd(int i) { return i % 2 == 1; } // Container: { 0, 1, 2, 3, 14, 15 } partition_point(c, lessThan10); // --> c.begin() + 4 (pointing at 14) partition_point(c.begin(), c.end(), lessThan10); // --> c.begin() + 4 (pointing at 14) partition_point(c.begin(), c.begin() + 3, lessThan10); // --> c.begin() + 3 (end) partition_point(c.end(), c.end(), isOdd); // --> c.end() (empty range) ``` -------------------------------- ### is_palindrome Algorithm Examples Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/Misc Demonstrates the usage of the is_palindrome algorithm with various container types and predicates. It handles empty ranges, single-element ranges, and different palindrome conditions. ```cpp const std::list empty; const std::vector singleElement{'z'}; int oddNonPalindrome[] = {3,2,2}; const int oddPalindrome[] = {1,2,3,2,1}; const int evenPalindrome[] = {1,2,2,1}; int evenNonPalindrome[] = {1,4,8,8}; is_palindrome(empty)) --> true //empty range is_palindrome(singleElement)) --> true is_palindrome(std::begin(oddNonPalindrome), std::end(oddNonPalindrome))) --> false is_palindrome(std::begin(evenPalindrome), std::end(evenPalindrome))) --> true is_palindrome(empty.begin(), empty.end(), functorComparator())) --> true //empty range is_palindrome(std::begin(oddNonPalindrome), std::end(oddNonPalindrome), funcComparator)) --> false is_palindrome(std::begin(oddPalindrome), std::end(oddPalindrome)) --> true is_palindrome(evenPalindrome, std::equal_to())) --> true is_palindrome(std::begin(evenNonPalindrome), std::end(evenNonPalindrome)) --> false is_palindrome("a") --> true is_palindrome("aba", std::equal_to()) --> true ``` -------------------------------- ### C++ mismatch algorithm examples with containers Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/CXX14 Illustrates the `mismatch` algorithm's behavior when applied to container iterators. It shows cases where the first elements differ, where a sub-sequence matches, and when dealing with empty sequences. ```cpp auto c1 = { 0, 1, 2, 3, 14, 15 }; auto c2 = { 1, 2, 3 }; mismatch ( c1.begin(), c1.end(), c2.begin(), c2.end()) --> mismatch ( c1.begin() + 1, c1.begin() + 4, c2.begin(), c2.end()) --> mismatch ( c1.end(), c1.end(), c2.end(), c2.end()) --> ``` -------------------------------- ### any_of Usage Examples with Predicates and Values - C++ Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/CXX11 Demonstrates any_of and any_of_equal functions on a container {0, 1, 2, 3, 14, 15} with custom predicates (isOdd, lessThan10) and value comparisons. Shows both iterator and range-based calls with expected boolean results. ```cpp bool isOdd ( int i ) { return i % 2 == 1; } bool lessThan10 ( int i ) { return i < 10; } using boost::algorithm; any_of ( c, isOdd ) --> true any_of ( c.begin (), c.end (), lessThan10 ) --> true any_of ( c.begin () + 4, c.end (), lessThan10 ) --> false any_of ( c.end (), c.end (), isOdd ) --> false // empty range any_of_equal ( c, 3 ) --> true any_of_equal ( c.begin (), c.begin () + 3, 3 ) --> false any_of_equal ( c.begin (), c.begin (), 99 ) --> false // empty range ``` -------------------------------- ### none_of Usage Examples with Predicates and Values - C++ Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/CXX11 Demonstrates none_of and none_of_equal functions on a container {0, 1, 2, 3, 14, 15} with custom predicates (isOdd, lessThan10) and value comparisons. Shows both iterator and range-based calls with expected boolean results. ```cpp bool isOdd ( int i ) { return i % 2 == 1; } bool lessThan10 ( int i ) { return i < 10; } using boost::algorithm; none_of ( c, isOdd ) --> false none_of ( c.begin (), c.end (), lessThan10 ) --> false none_of ( c.begin () + 4, c.end (), lessThan10 ) --> true none_of ( c.end (), c.end (), isOdd ) --> true // empty range none_of_equal ( c, 3 ) --> false none_of_equal ( c.begin (), c.begin () + 3, 3 ) --> true none_of_equal ( c.begin (), c.begin (), 99 ) --> true // empty range ``` -------------------------------- ### Generate Increasing Sequence with boost::algorithm::iota_n Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/iota_8hpp_1a83f90b863f3df8101d3ad981c0714261 The `boost::algorithm::iota_n` function generates a sequence of increasing values starting from a given initial value and writes them to an output iterator. It requires the `` header. The function takes an output iterator, an initial value, and the number of items to generate as parameters. ```C++ // In header: template BOOST_CXX14_CONSTEXPR OutputIterator iota_n(OutputIterator out, T value, std::size_t n); ``` -------------------------------- ### C++14 std::equal Algorithm Example Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/CXX14 Demonstrates the usage of the std::equal algorithm with different iterator configurations, highlighting the behavior with C++14 enhancements and potential undefined behavior in older versions. It shows how to compare sequences and the impact of iterator ranges. ```cpp auto seq1 = { 0, 1, 2 }; auto seq2 = { 0, 1, 2, 3, 4 }; std::equal ( seq1.begin (), seq1.end (), seq2.begin ()); // true std::equal ( seq2.begin (), seq2.end (), seq1.begin ()); // Undefined behavior std::equal ( seq1.begin (), seq1.end (), seq2.begin (), seq2.end ()); // false ``` -------------------------------- ### Copy Exactly N Elements with Boost Algorithm copy_n (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/copy__n_8hpp_1a6ca80976a0e4ff4dec4233c3fcf5735c Copies exactly n (n > 0) elements from the range starting at 'first' to the range starting at 'result'. This function is part of the C++2011 standard library and requires the header. It takes input iterators, a size, and an output iterator, returning the updated output iterator. ```cpp #include #include #include int main() { std::vector source = {1, 2, 3, 4, 5}; std::vector destination(5); auto end_it = boost::algorithm::copy_n(source.begin(), 3, destination.begin()); // destination will contain {1, 2, 3} and end_it will point to the fourth element. return 0; } ``` -------------------------------- ### Generate Increasing Sequence with iota (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/iota_8hpp_1a2d9177f87e32440dd85bf4af7b7b8836 The boost::algorithm::iota function generates an increasing sequence of values starting from an initial value and stores them in the range specified by the iterators. It is part of the C++2011 standard library. The function takes three parameters: 'first' (start iterator), 'last' (end iterator), and 'value' (the initial value of the sequence). ```cpp // In header: template BOOST_CXX14_CONSTEXPR void iota(ForwardIterator first, ForwardIterator last, T value); ``` -------------------------------- ### C++ std::mismatch examples with different iterator counts Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/CXX14 Demonstrates the usage of `std::mismatch` with both pre-C++14 (3-iterator) and C++14 (4-iterator) variants. Highlights the potential for undefined behavior with the older variant when sequences have different lengths and shows the correct usage of the C++14 variant. ```cpp auto seq1 = { 0, 1, 2 }; auto seq2 = { 0, 1, 2, 3, 4 }; std::mismatch ( seq1.begin (), seq1.end (), seq2.begin ()); // <3, 3> std::mismatch ( seq2.begin (), seq2.end (), seq1.begin ()); // Undefined behavior std::mismatch ( seq1.begin (), seq1.end (), seq2.begin (), seq2.end ()); // <3, 3> ``` -------------------------------- ### Reverse iterator approach comparison Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/Misc This example demonstrates the less efficient approach of using std::find with reverse iterators compared to the find_backward variants. The reverse iterator approach requires additional typing and is slower due to operator-- calls in member functions. ```C++ // Raw loop approach while (last-- != first) { if (*last == x) { // Use last here... } } // Using reverse iterators (less efficient) auto rfirst = std::make_reverse_iterator(last); auto rlast = std::make_reverse_iterator(first); auto it = std::find(rfirst, rlast, x); // Use it here... ``` -------------------------------- ### Check Sequence Permutation with Boost C++ Algorithms Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/cxx14_2is__permutation_8hpp This C++ code demonstrates the use of the `is_permutation` function from the Boost algorithm library. It checks if one range is a permutation of another, with overloads for custom comparison predicates. This requires the Boost C++ Libraries to be installed and configured. ```cpp namespace boost { namespace algorithm { template bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); template bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); } } ``` -------------------------------- ### Boost.Algorithm iota Function Signatures (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/iota_8hpp Defines the function signatures for the `iota` and `iota_n` functions within the Boost.Algorithm library. These functions are used to generate an increasing series of values starting from a given value. They support iterators, ranges, and a count for the number of elements to generate. The `BOOST_CXX14_CONSTEXPR` keyword indicates compile-time evaluation capabilities. ```cpp namespace boost { namespace algorithm { template BOOST_CXX14_CONSTEXPR void iota(ForwardIterator, ForwardIterator, T); template BOOST_CXX14_CONSTEXPR void iota(Range &, T); template BOOST_CXX14_CONSTEXPR OutputIterator iota_n(OutputIterator, T, std::size_t); } } ``` -------------------------------- ### Generate increasing sequence with boost::algorithm::iota Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/iota_8hpp_1a79e5b0fb12705726211c0ecebab2c33d Function template that generates an increasing sequence of values starting from a given initial value and stores them in the provided input range. This template is constexpr-compatible (CXX14) and requires the range and initial value as parameters. The function modifies the range in-place. ```cpp template BOOST_CXX14_CONSTEXPR void iota(Range & r, T value); ``` -------------------------------- ### Get Partition Point - C++ Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/partition__point_8hpp_1a281fc2fe41da071125988f63bf75f64d The boost::algorithm::partition_point function template identifies the boundary in a partitioned range. It takes a range and a predicate as input and returns an iterator to the first element that does not satisfy the predicate. Ensure the range is properly partitioned before calling this function. The header must be included. ```cpp // In header: template boost::range_iterator< Range >::type partition_point(Range & r, Predicate p); ``` -------------------------------- ### C++ is_permutation: Test Sequence Permutation Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/cxx11_2is__permutation_8hpp_1ae283e4f1f72ce39ce6f1d8b6fc522127 Tests if the sequence [first1, last1) is a permutation of the sequence starting at first2 using a binary predicate. Requires the header. It takes iterators for both sequences and a binary predicate for comparison. ```cpp // In header: template bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, BinaryPredicate p); ``` -------------------------------- ### Updating Boyer-Moore Searcher Return Value (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/Searching Demonstrates how to update older code that expected a single iterator return value from the Boyer-Moore searcher's operator() to the current interface which returns a pair of iterators. The `.first` field of the returned pair should be accessed to get the single iterator. ```C++ // Old code: // iterator foo = searcher(a, b); // New code: iterator foo = searcher(a, b).first; ``` -------------------------------- ### Apply Functor to Sequence Elements with Count - Boost C++ Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/for__each__n_8hpp Template function for_each_n applies a given functor to the first N elements of a sequence starting from an input iterator. It returns an iterator pointing to the element following the last processed element. This function is part of the Boost Algorithm library and requires an InputIterator, a Size parameter, and a Function object. ```cpp namespace boost { namespace algorithm { template InputIterator for_each_n(InputIterator, Size, Function); } } ``` -------------------------------- ### Boost Algorithm for_each_n Function Template (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/for__each__n_8hpp_1ac8a7b718e68d9f54c382fd6eb8dc3a56 Applies a function to the first N elements of a range. The return value of the function is ignored. It takes iterators defining the start and end of the range, the number of elements to process, and the function to apply. It returns an iterator to the element following the processed range. ```cpp #include // Example Usage: // std::vector data = {1, 2, 3, 4, 5}; // boost::algorithm::for_each_n(data.begin(), 3, [](int& x) { x *= 2; }); ``` -------------------------------- ### Boost Algorithm Mismatch Function Template C++ Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/mismatch_8hpp_1a7ff787a12f3c0b8c6901569bdde01c0b The boost::algorithm::mismatch function template compares two ranges element by element using a provided binary predicate. It returns a pair of iterators pointing to the first non-matching elements. This function requires the header and accepts iterators defining the start and end of both ranges, along with a binary predicate for comparison. ```cpp // In header: template BOOST_CXX14_CONSTEXPR std::pair< InputIterator1, InputIterator2 > mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred); ``` -------------------------------- ### boost::algorithm::is_permutation Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/cxx11_2is__permutation_8hpp_1ad36231afcf0cb3103b0f0434bb405162 Tests to see if the sequence [first1, last1) is a permutation of the sequence starting at first2. This function is part of the C++2011 standard library. ```APIDOC ## Function template is_permutation ### Description Tests to see if the sequence [first1, last1) is a permutation of the sequence starting at first2. This function is part of the C++2011 standard library. ### Synopsis ```cpp // In header: template bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2); ``` ### Parameters - **first1** (ForwardIterator1) - The start of the input sequence. - **last1** (ForwardIterator1) - The end of the input sequence. - **first2** (ForwardIterator2) - The start of the second sequence. ### Return Value `true` if the first sequence is a permutation of the second, `false` otherwise. ### Example ```cpp #include #include #include int main() { std::vector v1 = {1, 2, 3}; std::vector v2 = {3, 1, 2}; std::vector v3 = {1, 2, 4}; if (boost::algorithm::is_permutation(v1.begin(), v1.end(), v2.begin())) { std::cout << "v1 is a permutation of v2" << std::endl; } if (!boost::algorithm::is_permutation(v1.begin(), v1.end(), v3.begin())) { std::cout << "v1 is not a permutation of v3" << std::endl; } return 0; } ``` ``` -------------------------------- ### Test Sequence Permutation with is_permutation (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/cxx14_2is__permutation_8hpp_1a92f944ba39a3e4df4e6ff1d8a902a1c1 Tests if the sequence [first1, last1) is a permutation of the sequence starting at first2. This function is part of the C++2014 standard library. It requires forward iterators for both sequences. ```C++ // In header: template bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); ``` -------------------------------- ### Check Range Permutation - C++ Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/cxx11_2is__permutation_8hpp Template function that checks if a range is a permutation of another sequence starting from a forward iterator. This overload accepts a range object and a forward iterator for convenient range-based comparisons. ```cpp template bool is_permutation(const Range &, ForwardIterator); ``` -------------------------------- ### Test Sequence Permutation with is_permutation Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/cxx11_2is__permutation_8hpp_1a7efe794b353d4eb0a6112a8a0e94535c Checks if the sequence defined by a range is a permutation of another sequence starting from a given iterator. It requires the header. The function returns a boolean value indicating whether the permutation condition is met. ```cpp // In header: template bool is_permutation(const Range & r, ForwardIterator first2); ``` -------------------------------- ### Boost copy_until Function Template Synopsis Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/copy__if_8hpp_1a6c486177fc7627129066d427306f9413 This snippet shows the synopsis for the `boost::algorithm::copy_until` function template. It takes an input range, an output iterator, and a predicate function. The function copies elements from the input range to the output iterator as long as the predicate returns false for each element. The header file required is ``. ```cpp // In header: template BOOST_CXX14_CONSTEXPR std::pair< typename boost::range_iterator< const Range >::type, OutputIterator > copy_until(const Range & r, OutputIterator result, Predicate p); ``` -------------------------------- ### all_of Function Template in C++ Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/all__of_8hpp_1a4d8a7d950ae23b3b19247441a3dcbe5e The `all_of` function template from Boost.Algorithm checks if all elements within a given range satisfy a specified predicate. It returns true for an empty range and requires a range and a predicate as input. This function is defined in the `` header. ```c++ // In header: template BOOST_CXX14_CONSTEXPR bool all_of(const Range & r, Predicate p); ``` -------------------------------- ### apply_permutation and apply_reverse_permutation Interface (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/Misc Defines the interface for the apply_permutation and apply_reverse_permutation algorithms. These algorithms are available in two forms: one taking four iterators and another taking two ranges. They rearrange an item sequence based on an index sequence. ```cpp template void apply_permutation(RandomAccessIterator1 item_begin, RandomAccessIterator1 item_end, RandomAccessIterator2 ind_begin, RandomAccessIterator2 ind_end); template void apply_permutation(Range1& item_range, Range2& ind_range); template void apply_reverse_permutation(RandomAccessIterator1 item_begin, RandomAccessIterator1 item_end, RandomAccessIterator2 ind_begin, RandomAccessIterator2 ind_end); template void apply_reverse_permutation(Range1& item_range, Range2& ind_range); ``` -------------------------------- ### Boost Algorithm copy_if Synopsis Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/copy__if_8hpp_1a07e439504ff98fc8c4bb3f588a7db686 The synopsis for the `boost::algorithm::copy_if` function template, which copies elements from an input range to an output range if they satisfy a given predicate. It requires the `` header and takes input iterators, an output iterator, and a predicate as parameters. ```cpp // In header: template BOOST_CXX14_CONSTEXPR OutputIterator copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate p); ``` -------------------------------- ### Boost C++ knuth_morris_pratt Function Call Operator (Range) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/classboost_1_1algorithm_1_1knuth__morris__pratt The overloaded function call operator for knuth_morris_pratt that accepts a range object. It returns a pair of iterators indicating the start and end of the first match within the range. ```cpp template std::pair< typename boost::range_iterator< Range >::type, typename boost::range_iterator< Range >::type > operator()(Range & r) const; ``` -------------------------------- ### for_each_n - Apply Functor to Sequence Elements Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/for__each__n_8hpp The for_each_n template function applies a functor to elements of a sequence. It processes a specified number of elements starting from an input iterator and returns an iterator to the element following the last processed element. ```APIDOC ## for_each_n Template Function ### Description Applies a functor to the elements of a sequence for a specified number of iterations. This is a C++17 compatible function that processes exactly N elements from the input sequence. ### Signature ```cpp namespace boost { namespace algorithm { template InputIterator for_each_n(InputIterator, Size, Function); } } ``` ### Template Parameters - **InputIterator** (type) - Required - An input iterator type pointing to elements in the sequence - **Size** (type) - Required - An integral type representing the number of elements to process - **Function** (type) - Required - A callable type (functor, function pointer, or lambda) that accepts elements from the sequence ### Parameters - **first** (InputIterator) - Required - Iterator pointing to the first element to process - **n** (Size) - Required - The number of elements to apply the functor to - **f** (Function) - Required - The functor or function to apply to each element ### Return Value - **InputIterator** - An iterator pointing to the element following the last processed element. If n is less than or equal to 0, returns the input iterator unchanged. ### Usage Example ```cpp #include #include #include int main() { std::vector v = {1, 2, 3, 4, 5}; // Apply a lambda to the first 3 elements auto it = boost::algorithm::for_each_n( v.begin(), 3, [](int& x) { x *= 2; } ); // it now points to the 4th element (v[3]) return 0; } ``` ### Notes - Part of the Boost.Algorithm library - Introduced for C++17 compatibility - The functor can modify elements if the iterator supports modification - Header: `` - Author: Marshall Clow - License: Boost Software License 1.0 ``` -------------------------------- ### Boyer-Moore-Horspool Procedural Interface (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/the_boost_algorithm_library/Searching/BoyerMooreHorspool The procedural interface for the Boyer-Moore-Horspool algorithm. It performs table building and searching in a single step. Suitable for single searches or when table pre-computation is not needed. ```cpp template pair boyer_moore_horspool_search ( corpusIter corpus_first, corpusIter corpus_last, patIter pat_first, patIter pat_last ); ``` -------------------------------- ### Boost C++ knuth_morris_pratt Function Call Operator (Iterators) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/classboost_1_1algorithm_1_1knuth__morris__pratt The overloaded function call operator for knuth_morris_pratt that accepts two iterators defining the corpus to search within. It returns a pair of iterators indicating the start and end of the first match. ```cpp template std::pair< corpusIter, corpusIter > operator()(corpusIter corpus_first, corpusIter corpus_last) const; ``` -------------------------------- ### Check if all elements match a value or predicate using Boost.Algorithm Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/all__of_8hpp This C++ code demonstrates the usage of `all_of` and `all_of_equal` from the Boost.Algorithm library. These functions test ranges to determine if all elements meet a specified predicate or match a given value. They are useful for validating data integrity and applying conditions across collections. ```cpp namespace boost { namespace algorithm { template BOOST_CXX14_CONSTEXPR bool all_of(InputIterator, InputIterator, Predicate); template BOOST_CXX14_CONSTEXPR bool all_of(const Range &, Predicate); template BOOST_CXX14_CONSTEXPR bool all_of_equal(InputIterator, InputIterator, const T &); template BOOST_CXX14_CONSTEXPR bool all_of_equal(const Range &, const T &); } } ``` -------------------------------- ### Boost Algorithm not_enough_input Exception Definition Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/structboost_1_1algorithm_1_1not__enough__input Defines the `not_enough_input` exception structure, which inherits from `boost::algorithm::hex_decode_error`. This exception is thrown when an input sequence terminates prematurely during hexadecimal decoding operations. It is declared in the `` header. ```cpp #include struct not_enough_input : public boost::algorithm::hex_decode_error { }; ``` -------------------------------- ### Boyer-Moore Object-Oriented Interface (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/Searching Implements the Boyer-Moore search algorithm using an object-oriented approach. The constructor precomputes search tables for a given pattern. The operator() performs the search on a corpus. This is efficient for searching the same pattern in multiple corpora. ```C++ template class boyer_moore { public: boyer_moore ( patIter first, patIter last ); ~boyer_moore (); template pair operator () ( corpusIter corpus_first, corpusIter corpus_last ); }; ``` -------------------------------- ### Boost is_permutation Function Template (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/cxx14_2is__permutation_8hpp_1aaac2ab337ba01d152cdca1aea8ba49a0 Tests if the sequence [first1, last1) is a permutation of the sequence starting at first2. This function is part of the C++2014 standard library and requires the `` header. It takes four iterators defining the two sequences and a binary predicate for element comparison. ```cpp #include template bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); ``` -------------------------------- ### Partition Copy Algorithm - C++ Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/partition__copy_8hpp The `partition_copy` algorithm in Boost C++ Libraries copies elements from an input sequence to two separate output sequences. Elements for which the predicate returns true are copied to the first output iterator, and elements for which it returns false are copied to the second. It operates on iterators or ranges and requires a unary predicate function. ```cpp namespace boost { namespace algorithm { template BOOST_CXX14_CONSTEXPR std::pair< OutputIterator1, OutputIterator2 > partition_copy(InputIterator, InputIterator, OutputIterator1, OutputIterator2, UnaryPredicate); template BOOST_CXX14_CONSTEXPR std::pair< OutputIterator1, OutputIterator2 > partition_copy(const Range &, OutputIterator1, OutputIterator2, UnaryPredicate); } } ``` -------------------------------- ### Boost.Algorithm unhex function interface Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/Misc The unhex algorithm is the inverse of hex, converting hexadecimal characters back into a sequence of values. It accepts input through iterator pairs, null-terminated pointers, or Boost.Range objects, writing the decoded values to an output iterator. ```cpp template OutputIterator unhex(InputIterator first, InputIterator last, OutputIterator out); template OutputIterator unhex(const T *ptr, OutputIterator out); template OutputIterator unhex(const Range &r, OutputIterator out); ``` -------------------------------- ### Boyer-Moore Procedural Interface (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/Searching Provides a procedural interface for the Boyer-Moore search algorithm. This function takes iterators for both the corpus and the pattern, performing the search in a single step. It's suitable for one-off searches or when the pattern changes frequently. ```C++ template pair boyer_moore_search ( corpusIter corpus_first, corpusIter corpus_last, patIter pat_first, patIter pat_last ); ``` -------------------------------- ### Hexadecimal Conversion Functions (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/Misc The 'hex.hpp' header defines functions for converting between hexadecimal character sequences and their corresponding integer or character representations. It supports various output iterators and includes error handling for invalid input. The functions run in linear time and provide strong exception guarantees. ```cpp #include // Exception classes struct hex_decode_error: virtual boost::exception, virtual std::exception {}; struct not_enough_input : public hex_decode_error; struct non_hex_input : public hex_decode_error; // Example Usage (assuming 'out' is an appropriate iterator) // hex("abcdef", out); // unhex("616263646566", out); ``` -------------------------------- ### Boost Algorithm: Sort and Partition Subranges in C++ Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/sort__subrange_8hpp Provides C++ functions to sort and partition a specified subrange within a larger range. It overloads functions to accept an optional binary predicate for custom comparison logic. These functions are part of the Boost Algorithm library, found in the header. ```C++ namespace boost { namespace algorithm { template void sort_subrange(Iterator first, Iterator last, Iterator sub_first, Iterator sub_last, Pred p); template void sort_subrange(Iterator first, Iterator last, Iterator sub_first, Iterator sub_last); template void partition_subrange(Iterator first, Iterator last, Iterator sub_first, Iterator sub_last, Pred p); template void partition_subrange(Iterator first, Iterator last, Iterator sub_first, Iterator sub_last); } } ``` -------------------------------- ### Boost Algorithm is_permutation Function Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/cxx11_2is__permutation_8hpp_1ad36231afcf0cb3103b0f0434bb405162 Tests if the sequence [first1, last1) is a permutation of the sequence starting at first2. This function is part of the C++2011 standard library. It requires iterators for both sequences as input and returns a boolean indicating whether the permutation condition is met. The sequences being compared should have the same length for a meaningful permutation check. ```cpp // In header: template bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2); ``` -------------------------------- ### C++ Boost Algorithm is_permutation Function Template Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/cxx11_2is__permutation_8hpp_1a47b63c32c08a7d025059b1d3df3ff17a Tests if the sequence [first, last) is a permutation of the sequence starting at first2. It requires the Boost C++ Libraries and uses a binary predicate for element comparison. The function returns a boolean value indicating whether the permutation test is true or false. ```cpp // In header: template boost::disable_if_c< boost::is_same< Range, ForwardIterator >::value, bool >::type is_permutation(const Range & r, ForwardIterator first2, BinaryPredicate pred); ``` -------------------------------- ### partition_point - Find partition boundary with forward iterators Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/CXX11 Locates the first element in a partitioned sequence that does not satisfy a given predicate. Supports two forms: iterator-based (two iterators) and range-based (using Boost.Range). Runs in O(log N) time with strong exception guarantee. Available in C++11 standard library. ```C++ template ForwardIterator partition_point(ForwardIterator first, ForwardIterator last, Predicate p); template boost::range_iterator partition_point(const Range &r, Predicate p); ``` -------------------------------- ### Boost Algorithm Equal Function Template (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/equal_8hpp_1a273587c9ff2e7157ee71e346f0390040 The `boost::algorithm::equal` function template checks if two ranges have identical elements based on a provided binary predicate. It requires iterators defining the start and end of both ranges and a predicate function for element comparison. Returns true if all elements match, false otherwise. ```cpp // In header: template BOOST_CXX14_CONSTEXPR bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred); ``` -------------------------------- ### Boost KMP Procedural Interface (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/the_boost_algorithm_library/Searching/KnuthMorrisPratt The procedural interface for the Knuth-Morris-Pratt algorithm. It performs table building and searching in a single function call. Suitable for one-off searches or when the pattern changes frequently. ```cpp template pair knuth_morris_pratt_search ( corpusIter corpus_first, corpusIter corpus_last, patIter pat_first, patIter pat_last ); ``` -------------------------------- ### Find Mismatch in Two Ranges (C++14) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/mismatch_8hpp_1a6f15e19b3db0e15b328085be6caacac8 The boost::algorithm::mismatch function template, available in ``, compares two ranges defined by iterators and returns a pair of iterators pointing to the first elements that do not match. It requires C++14 support and takes four iterators defining the start and end of both ranges as input. ```cpp #include #include template BOOST_CXX14_CONSTEXPR std::pair< InputIterator1, InputIterator2 > mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); ``` -------------------------------- ### Check if any element satisfies predicate (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/any__of_8hpp_1acfc1b095315fdb763c7cd0cbda15d0d8 The `any_of` function template iterates through a range and applies a predicate to each element. It returns true as soon as the predicate returns true for any element. If the range is empty or no element satisfies the predicate, it returns false. This function is part of the Boost C++ Libraries and requires the `` header. ```cpp // In header: template BOOST_CXX14_CONSTEXPR bool any_of(const Range & r, Predicate p); ``` -------------------------------- ### exclusive_scan Template Functions - Boost Algorithm C++ Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/exclusive__scan_8hpp Template functions for performing exclusive prefix scan operations on input ranges. Supports both custom binary operations and default addition. The function accumulates values starting from an initial value, writing results to an output iterator without including the current element in its own computation. ```cpp namespace boost { namespace algorithm { template OutputIterator exclusive_scan(InputIterator first, InputIterator last, OutputIterator result, T init, BinaryOperation bOp); template OutputIterator exclusive_scan(InputIterator first, InputIterator last, OutputIterator result, T init); } } ``` -------------------------------- ### Test Range Elements with Predicate - Boost C++ Algorithm Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/any__of_8hpp Template functions that test if any elements in a range satisfy a predicate or match a specific value. Provides both iterator-pair and range-based interfaces with BOOST_CXX14_CONSTEXPR support for compile-time evaluation. The any_of function accepts a predicate, while any_of_equal matches against a specific value. ```C++ namespace boost { namespace algorithm { template BOOST_CXX14_CONSTEXPR bool any_of(InputIterator, InputIterator, Predicate); template BOOST_CXX14_CONSTEXPR bool any_of(const Range &, Predicate); template BOOST_CXX14_CONSTEXPR bool any_of_equal(InputIterator, InputIterator, const V &); template BOOST_CXX14_CONSTEXPR bool any_of_equal(const Range &, const V &); } } ``` -------------------------------- ### Boost Algorithm copy_if_until Function Template (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/copy__if_8hpp_1a459d1a926144a045039f887d0e7e6b6e Copies elements from an input range to an output iterator based on a copy predicate, stopping when a termination predicate is satisfied. Requires the header. It takes an input range, an output iterator, a copy predicate, and a termination predicate, returning the updated output iterator. ```cpp // In header: template BOOST_CXX14_CONSTEXPR std::pair< typename boost::range_iterator< const Range >::type, OutputIterator > copy_if_until(const Range & r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred); ``` -------------------------------- ### transform_inclusive_scan Template Function with Initial Value - C++ Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/transform__inclusive__scan_8hpp Template function that performs an inclusive scan with unary transformation and binary operation on a range, starting with an initial value. The function transforms each element via the unary operation, then combines results using the binary operation in an inclusive manner, storing output to the specified iterator. ```C++ namespace boost { namespace algorithm { template OutputIterator transform_inclusive_scan(InputIterator, InputIterator, OutputIterator, BinaryOperation, UnaryOperation, T); } } ``` -------------------------------- ### Knuth-Morris-Pratt Search Function (C++) Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/doxygen/knuth__morris__pratt_8hpp_1a03369098894b6abfd0cbb9c6e851e78c Implements the Knuth-Morris-Pratt string searching algorithm to find occurrences of a pattern within a corpus. It requires random access iterators for both the corpus and the pattern. The function returns a pair of iterators indicating the start and end of the first match found, or the end iterator of the corpus if no match is found. It is defined in the `` header. ```cpp #include #include template std::pair knuth_morris_pratt_search(corpusIter corpus_first, corpusIter corpus_last, patIter pat_first, patIter pat_last); ``` -------------------------------- ### is_partitioned_until Algorithm Interface Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/Misc Defines the interface for the is_partitioned_until algorithm, which tests if a sequence is partitioned according to a predicate. It returns the iterator to the first element that does not satisfy the predicate. ```cpp template InputIterator is_partitioned_until ( InputIterator first, InputIterator last, Predicate p ); template typename boost::range_iterator::type is_partitioned_until ( const Range &r, Predicate p ); ``` -------------------------------- ### any_of Algorithm Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/CXX11 The 'any_of' algorithm tests if any element in a sequence satisfies a given property (predicate). The 'any_of_equal' algorithm tests if any element in a sequence is equal to a specific value. Both functions are available with iterator pairs or Boost.Range compatible ranges. ```APIDOC ## any_of and any_of_equal ### Description Tests if any element in a sequence satisfies a property (predicate) or is equal to a given value. ### Method `any_of(InputIterator first, InputIterator last, Predicate p)` `any_of(const Range &r, Predicate p)` `any_of_equal(InputIterator first, InputIterator last, V const &val)` `any_of_equal(const Range &r, V const &val)` ### Endpoint N/A (Library functions) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp // Assuming 'c' is a container like { 0, 1, 2, 3, 14, 15 } bool isOdd ( int i ) { return i % 2 == 1; } using boost::algorithm; any_of ( c, isOdd ) --> true ``` ### Response #### Success Response (200) `bool`: Returns `true` if at least one element satisfies the condition, `false` otherwise. #### Response Example ```json { "result": true } ``` ### Iterator Requirements All iterators except output iterators. ### Complexity O(N) linear time. Terminates early if a condition is met. ### Exception Safety Strong exception guarantee. ``` -------------------------------- ### C++ mismatch function signatures Source: https://www.boost.org/doc/libs/latest/libs/algorithm/doc/html/algorithm/CXX14 Presents the interface signatures for the `mismatch` algorithm. It includes two overloads: one using `std::equal_to` for comparison and another that accepts a custom binary predicate. ```cpp template std::pair mismatch ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2 ); template std::pair mismatch ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred ); ```