### Combine Function Example (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/utilities/combine An example demonstrating the usage of the combine function with a std::vector and std::list. It shows how to iterate over paired elements from both ranges using BOOST_FOREACH and boost::tie, printing the combined output. ```cpp #include #include #include #include #include int main(int, const char*[]) { std::vector v; std::list l; for (int i = 0; i < 5; ++i) { v.push_back(i); l.push_back(static_cast(i) + 'a'); } int ti; char tc; BOOST_FOREACH(boost::tie(ti, tc), boost::combine(v, l)) { std::cout << '(' << ti << ',' << tc << ')' << '\n'; } return 0; } ``` -------------------------------- ### Boost.Range sliced adaptor C++ example Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/reference/sliced Demonstrates how to use the boost::adaptors::sliced and boost::adaptors::slice functionalities to create a sub-range from a std::vector. This example requires the Boost.Range library and includes necessary headers for adaptors, algorithms, assignment, and I/O. ```cpp #include #include #include #include #include #include int main(int argc, const char* argv[]) { using namespace boost::adaptors; using namespace boost::assign; std::vector input; input += 1,2,3,4,5,6,7,8,9; boost::copy( input | sliced(2, 5), std::ostream_iterator(std::cout, ",")); return 0; } ``` -------------------------------- ### Use ref_unwrapped adaptor with C++ Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/reference/ref_unwrapped Demonstrates the usage of the boost::adaptors::ref_unwrapped adaptor in C++. This example iterates over a vector of std::reference_wrapper and prints the 'value' member of each unwrapped 'example' object. It requires the header. ```cpp #include #include #include struct example { int value; }; int main(int argc, const char* argv[]) { using boost::adaptors::ref_unwrapped; example one{1}; example two{2}; example three{3}; std::vector > input{one, two, three}; for (auto&& entry : input | ref_unwrapped) { std::cout << entry.value; } return 0; } ``` -------------------------------- ### Example of Boost.Range join function Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/utilities/join This example demonstrates how to use the `join` function from Boost.Range. It shows the creation of a joined range by concatenating two integer ranges generated by `irange`. The expected output is a range representing integers from 0 up to (but not including) 10. ```cpp join(irange(0,5), irange(5,10)) ``` -------------------------------- ### Applying Reversed Adaptor to a Vector Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/introduction Demonstrates how to use the `boost::adaptors::reversed` adaptor with `operator|` to iterate over a vector in reverse order. This example requires including `` and ``. ```C++ #include #include #include #include std::vector vec; boost::copy( vec | boost::adaptors::reversed, std::ostream_iterator(std::cout) ); ``` -------------------------------- ### Chaining Range Adaptors for Composition Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/introduction Illustrates the flexibility of Range Adaptors by chaining multiple adaptors. This example shows how to combine `boost::adaptors::replaced_if` with `boost::adaptors::reversed` to create a new algorithm on the fly, enhancing code composition and expressiveness. ```C++ std::vector vec; boost::push_back(vec, rng | boost::adaptors::replaced_if(pred, new_value) | boost::adaptors::reversed); ``` -------------------------------- ### Boost Range strided Adaptor Usage Example (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/reference/strided Demonstrates how to use the boost::adaptors::strided functionality to create a strided view of a std::vector and copy its elements to standard output. This example requires the Boost.Range library and standard C++ iterators. ```C++ #include #include #include #include #include #include int main(int argc, const char* argv[]) { using namespace boost::adaptors; using namespace boost::assign; std::vector input; input += 1,2,3,4,5,6,7,8,9,10; boost::copy( input | strided(2), std::ostream_iterator(std::cout, ",")); return 0; } ``` -------------------------------- ### Boost Range fill_n Algorithm Prototype Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/algorithms/mutating/fill_n This is the C++ prototype for the fill_n algorithm from the Boost Range library. It takes a mutable ForwardRange, a size, and a value, and assigns the value to 'n' elements starting from the beginning of the range. ```cpp template ForwardRange& fill( ForwardRange& rng, Size n, const Value& val ); ``` -------------------------------- ### Boost C++ Libraries irange Function Prototypes Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/ranges/irange This section details the various prototypes for the irange function in Boost C++ Libraries. It specifies how to generate integer ranges with different parameters, including start, end, and step size. The function requires Integer to model the Integer Concept and StepSize to model the SignedInteger Concept. It returns an iterator_range. ```cpp template iterator_range< range_detail::integer_iterator > irange(Integer last); template iterator_range< range_detail::integer_iterator > irange(Integer first, Integer last); template iterator_range< range_detail::integer_iterator_with_step > irange(Integer first, Integer last, StepSize step_size); ``` -------------------------------- ### Iterate MFC/ATL Containers with BOOST_FOREACH and Range Algorithms Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/mfc_atl Demonstrates using BOOST_FOREACH macro to iterate over nested MFC/ATL containers (CTypedPtrArray and CList) and applying Boost.Range algorithms like boost::to_upper, boost::begin, and boost::end to MFC string types. This example shows how the MFC/ATL extension enables seamless integration of Boost.Range functionality with MFC/ATL collection types. ```cpp CTypedPtrArray *> myArray; ... BOOST_FOREACH (CList *theList, myArray) { BOOST_FOREACH (CString& str, *theList) { boost::to_upper(str); std::sort(boost::begin(str), boost::end(str)); ... } } ``` -------------------------------- ### Reversed Range Adaptor - Boost C++ Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/reference/reversed Demonstrates using the Boost reversed adaptor to iterate through a vector in reverse order. The example uses both the pipe operator syntax with the adaptor and the copy algorithm to output reversed elements. This adaptor works with any bidirectional range and returns a boost::reversed_range object. ```cpp #include #include #include #include #include #include int main(int argc, const char* argv[]) { using namespace boost::adaptors; using namespace boost::assign; std::vector input; input += 1,2,3,4,5,6,7,8,9; boost::copy( input | reversed, std::ostream_iterator(std::cout, ",")); return 0; } ``` -------------------------------- ### C++ Boost.Range uniqued adaptor example Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/reference/uniqued Demonstrates the usage of the boost::adaptors::uniqued adaptor in C++ to filter duplicate adjacent elements from a std::vector and print the unique elements to standard output. Includes necessary headers for ranges, adaptors, copying, and I/O. ```cpp #include #include #include #include #include #include int main(int argc, const char* argv[]) { using namespace boost::assign; using namespace boost::adaptors; std::vector input; input += 1,1,2,2,2,3,4,5,6; boost::copy( input | uniqued, std::ostream_iterator(std::cout, ",")); return 0; } ``` -------------------------------- ### Iterate over map keys using Range adaptor Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/introduction Shows how to use the Boost Range library's map_keys adaptor to iterate over only the keys in a std::map. Like the map_values example, this uses the pipe operator to compose the adaptor with the for_each algorithm for straightforward key iteration. ```cpp using namespace boost; using namespace boost::adaptors; for_each( my_map | map_keys, fn ); ``` -------------------------------- ### copy_n Template Function Prototype - Boost C++ Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/algorithms/new/copy_n Defines the copy_n function template that copies exactly n elements from a SinglePassRange starting at boost::begin(rng) to an OutputIterator. Requires SinglePassRange model, Integer Size, and OutputIterator model as template parameters. Returns an OutputIterator pointing to the position after the last copied element. ```cpp template OutputIterator copy_n(const SinglePassRange& rng, Size n, OutputIterator out); ``` -------------------------------- ### Extract Values from Map using map_values Adaptor - C++ Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/reference/map_values Demonstrates using the map_values adaptor to extract and display values from a std::map. The adaptor works with ranges containing std::pair elements, returning only the second element (value) of each pair. This example creates a map with integer keys and values, then uses the pipe operator to apply map_values and copy the results to standard output. ```C++ #include #include #include #include #include #include #include int main(int argc, const char* argv[]) { using namespace boost::assign; using namespace boost::adaptors; std::map input; for (int i = 0; i < 10; ++i) input.insert(std::make_pair(i, i * 10)); boost::copy( input | map_values, std::ostream_iterator(std::cout, ",")); return 0; } ``` -------------------------------- ### Basic Range Algorithm Usage with boost::sort Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/algorithms/introduction Demonstrates the simplest form of range algorithms by replacing the traditional std::sort iterator-based call with boost::sort that accepts a range directly. This eliminates the need to pass both begin() and end() iterators. ```cpp #include #include std::vector vec = ...; boost::sort(vec); ``` -------------------------------- ### Chainable Range Algorithms with boost::sort and boost::unique Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/algorithms/introduction Shows how range algorithms can be chained together in functional style, applying boost::sort followed by boost::unique on the result. This demonstrates the seamless functional programming capability without intermediate variables. ```cpp boost::unique(boost::sort(vec)); ``` -------------------------------- ### Get Range Category Type (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/concept_implementation/semantics/metafunctions Determines the iterator category for a given range. This metafunction uses `range_iterator` to identify the iterator type and then queries its `iterator_category`. It is a compile-time operation. ```cpp #include #include // For a range X using category_type = boost::range_category::type; // Equivalent to: // using category_type = boost::iterator_category::type>::type; ``` -------------------------------- ### Boost.Range push_front Algorithm Prototype (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/algorithms/new/push_front This C++ prototype defines the signature for the `push_front` algorithm. It takes a container and a single-pass range as input, and returns the modified container. The elements from the `from` range are inserted at the beginning of the `target` container. The `Container` must support insertion at `begin()`, and `SinglePassRange`'s value type must be convertible to `Container`'s value type. ```cpp template< class Container, class SinglePassRange > Container& push_front(Container& target, const SinglePassRange& from); ``` -------------------------------- ### Get Range Iterator Type (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/concept_implementation/semantics/metafunctions Retrieves the iterator type for a given range. This metafunction determines the appropriate iterator, supporting both mutable and constant iterators. It is a compile-time operation. ```cpp #include // For a mutable range X using iterator_type = boost::range_iterator::type; // For a constant range X using const_iterator_type = boost::range_iterator::type; ``` -------------------------------- ### Composing Multiple Adaptors with Operator| Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/introduction Demonstrates the preferred, more readable syntax for applying multiple adaptors sequentially using the `operator|`. This chaining makes complex adaptor compositions concise and clear. ```C++ std::vector vec; boost::copy( vec | boost::adaptors::reversed | boost::adaptors::uniqued, std::ostream_iterator(std::cout) ); ``` -------------------------------- ### Get Range Difference Type (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/concept_implementation/semantics/metafunctions Calculates the difference type for iterators of a range. This metafunction uses `range_iterator` to determine the iterator type and then queries its `difference_type`. It operates at compile time. ```cpp #include #include // For a range X using difference_type = boost::range_difference::type; // Equivalent to: // using difference_type = boost::iterator_difference::type>::type; ``` -------------------------------- ### Get Range Reference Type (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/concept_implementation/semantics/metafunctions Obtains the reference type for elements within a range. Similar to `range_value`, this metafunction uses `range_iterator` to find the iterator and then queries its reference type. It is a compile-time operation. ```cpp #include #include // For a range X using reference_type = boost::range_reference::type; // Equivalent to: // using reference_type = boost::iterator_reference::type>::type; ``` -------------------------------- ### Get Range Pointer Type (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/concept_implementation/semantics/metafunctions Retrieves the pointer type for elements within a range. This metafunction leverages `range_iterator` to determine the iterator type and then queries its associated pointer type. It functions at compile time. ```cpp #include #include // For a range X using pointer_type = boost::range_pointer::type; // Equivalent to: // using pointer_type = boost::iterator_pointer::type>::type; ``` -------------------------------- ### Combine Function Synopsis (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/utilities/combine The synopsis for the combine function shows its namespace, template parameters, return type, and constructor. It highlights the dependency on Boost.Iterator for zip_iterator and Boost.Tuple for tuple creation. ```cpp namespace boost { namespace range { template class combined_range : public iterator_range > { public: combined_range(IterTuple first, IterTuple last); }; template auto combine(Ranges&&... rngs) -> combined_range } // namespace range } // namespace boost ``` -------------------------------- ### Get Range Value Type (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/concept_implementation/semantics/metafunctions Determines the value type of elements within a range. This metafunction relies on the `range_iterator` metafunction to find the iterator and then queries its associated value type. It operates at compile time. ```cpp #include #include // For a range X using value_type = boost::range_value::type; // Equivalent to: // using value_type = boost::iterator_value::type>::type; ``` -------------------------------- ### make_heap Algorithm Prototypes (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/algorithms/heap/make_heap Demonstrates the various prototypes for the make_heap algorithm in C++. These prototypes support creating heaps from ranges with or without a custom comparison predicate, and handle both mutable and const ranges. ```c++ template RandomAccessRange& make_heap(RandomAccessRange& rng); template const RandomAccessRange& make_heap(const RandomAccessRange& rng); template RandomAccessRange& make_heap(RandomAccessRange& rng, Compare pred); template const RandomAccessRange& make_heap(const RandomAccessRange& rng, Compare pred); ``` -------------------------------- ### Composing Multiple Adaptors with Function Calls Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/introduction Illustrates the less readable syntax for applying multiple adaptors sequentially using nested function calls. This approach becomes cumbersome quickly compared to the `operator|` syntax. ```C++ std::vector vec; boost::copy( boost::adaptors::unique( boost::adaptors::reverse( vec ) ), std::ostream_iterator(std::cout) ); ``` -------------------------------- ### Copy Unique Values Using Chained Range Algorithms Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/algorithms/introduction Demonstrates copying unique sorted values directly to output using chained boost::copy, boost::unique, and boost::sort operations. This pattern shows safe composition of range algorithms with output iterators. ```cpp boost::copy(boost::unique(boost::sort(vec)), std::ostream_iterator(std::cout)); ``` -------------------------------- ### Replace replace_copy_if with Range Adaptor and push_back Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/introduction Demonstrates replacing `boost::replace_copy_if` with a combination of Range Adaptors and `boost::push_back`. This approach avoids potential extra allocations associated with `std::back_inserter` and offers greater flexibility. ```C++ std::vector vec; boost::replace_copy_if( rng, std::back_inserter(vec), pred, new_value ); // Alternative using Range Adaptors and boost::push_back: std::vector vec; boost::push_back(vec, rng | boost::adaptors::replaced_if(pred, new_value)); ``` -------------------------------- ### Get Size of a Random Access Range (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/concepts/random_access_range Demonstrates how to obtain the number of elements in a Boost Random Access Range using `boost::size(a)`. This operation is guaranteed to complete in amortized constant time. The result is of type `boost::range_size::type`. ```cpp #include // Assuming 'my_range' is a model of Random Access Range // auto range_size = boost::size(my_range); ``` -------------------------------- ### partition Function Template - Boost Range Library Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/algorithms/mutating/partition Template function that partitions a ForwardRange based on a UnaryPredicate, returning an iterator to the partition point. Provides four overloads supporting mutable/const ranges and optional range_return_value configuration. Requires ForwardRange model and UnaryPredicate concept compliance with O(n) complexity and at most n/2 swaps. ```cpp template< class ForwardRange, class UnaryPredicate > typename range_iterator::type partition(ForwardRange& rng, UnaryPredicate pred); template< class ForwardRange, class UnaryPredicate > typename range_iterator::type partition(const ForwardRange& rng, UnaryPredicate pred); template< range_return_value re, class ForwardRange, class UnaryPredicate > typename range_return::type partition(ForwardRange& rng, UnaryPredicate pred); template< range_return_value re, class ForwardRange, class UnaryPredicate > typename range_return::type partition(const ForwardRange& rng, UnaryPredicate pred); ``` -------------------------------- ### Filter Elements in a Range using Boost C++ Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/reference/filtered Demonstrates filtering elements from a range using Boost C++'s 'filtered' adaptor. This adaptor requires a predicate function and returns a new range containing only elements for which the predicate evaluates to true. The example shows usage with a std::vector and outputs even numbers. ```cpp #include #include #include #include #include #include struct is_even { bool operator()( int x ) const { return x % 2 == 0; } }; int main(int argc, const char* argv[]) { using namespace boost::assign; using namespace boost::adaptors; std::vector input; input += 1,2,3,4,5,6,7,8,9; boost::copy( input | filtered(is_even()), std::ostream_iterator(std::cout, ",")); return 0; } ``` -------------------------------- ### Traditional STL Approach to Erase Duplicates Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/algorithms/introduction Shows the traditional STL pattern for erasing duplicate elements from a sorted container, requiring manual iterator management and boundary validation. Contrasts with the simpler boost range algorithm approach. ```cpp // assume 'vec' is already sorted std::vector::iterator i = std::unique(vec.begin(), vec.end()); // remember this check or you get into problems if (i != vec.end()) ++i; vec.erase(i, vec.end()); ``` -------------------------------- ### Boost.Range Functions for Range Operations (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/concept_implementation/synopsis Provides functions to perform common operations on C++ ranges, including obtaining iterators (begin, end, const_begin, const_end), checking for emptiness (empty), calculating distance and size, getting reverse iterators (rbegin, rend, const_rbegin, const_rend), and converting ranges to literal or array representations (as_literal, as_array). ```cpp namespace boost { // Single Pass Range functions template< class T > typename range_iterator::type begin( T& r ); template< class T > typename range_iterator::type begin( const T& r ); template< class T > typename range_iterator::type end( T& r ); template< class T > typename range_iterator::type end( const T& r ); template< class T > bool empty( const T& r ); // Forward Range functions template< class T > typename range_difference::type distance( const T& r ); template< class T > typename range_size::type size( const T& r ); // Bidirectional Range functions template< class T > typename range_reverse_iterator::type rbegin( T& r ); template< class T > typename range_reverse_iterator::type rbegin( const T& r ); template< class T > typename range_reverse_iterator::type rend( T& r ); template< class T > typename range_reverse_iterator::type rend( const T& r ); // Special const Range functions template< class T > typename range_iterator::type const_begin( const T& r ); template< class T > typename range_iterator::type const_end( const T& r ); template< class T > typename range_reverse_iterator::type const_rbegin( const T& r ); template< class T > typename range_reverse_iterator::type const_rend( const T& r ); // String utilities template< class T > iterator_range< ... see below ... > as_literal( T& r ); template< class T > iterator_range< ... see below ... > as_literal( const T& r ); template< class T > iterator_range< typename range_iterator::type > as_array( T& r ); template< class T > iterator_range< typename range_iterator::type > as_array( const T& r ); } // namespace 'boost' ``` -------------------------------- ### C++: Merge two sorted ranges Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/algorithms/mutating/merge The `merge` algorithm combines two sorted ranges (`rng1` and `rng2`) into a single sorted range starting at `out`. It can use either the default `operator<()` or a provided `BinaryPredicate` for element comparison. The function returns an `OutputIterator` pointing to the end of the merged range. It requires that the input ranges are sorted and do not overlap with the output range. ```cpp template< class SinglePassRange1, class SinglePassRange2, class OutputIterator > OutputIterator merge(const SinglePassRange1& rng1, const SinglePassRange2& rng2, OutputIterator out); template< class SinglePassRange1, class SinglePassRange2, class OutputIterator, class BinaryPredicate > OutputIterator merge(const SinglePassRange1& rng1, const SinglePassRange2& rng2, OutputIterator out, BinaryPredicate pred); ``` -------------------------------- ### Using Reversed Adaptor via Function Call Syntax Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/introduction Shows an alternative syntax for applying the `boost::adaptors::reversed` adaptor using a function call (`adaptors::reverse(rng)`). This achieves the same result as the `operator|` syntax but can be less readable when composing multiple adaptors. ```C++ std::vector vec; boost::copy( boost::adaptors::reverse(vec), std::ostream_iterator(std::cout) ); ``` -------------------------------- ### Erase Duplicates Except One Using boost::return_next_end Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/algorithms/introduction Demonstrates safely removing all duplicate elements while keeping one instance using boost::unique with boost::return_next_end parameter. Avoids the manual iterator boundary checking required in traditional STL approach. ```cpp boost::erase(vec, boost::unique(vec)); ``` -------------------------------- ### Boost.Range equal_range Algorithm (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/algorithms/non_mutating/equal_range The `equal_range` function from Boost.Range finds a sub-range within a sorted range where all elements are equal to a given value. It can operate with a default less-than comparison or a user-defined predicate. The function returns a pair of iterators defining the start and end of the matching sub-range. For unsorted ranges or when the range is not ordered, the behavior is undefined. The complexity is O(log N) for random-access ranges and O(N) otherwise. ```cpp #include #include #include int main() { std::vector v = {1, 2, 2, 2, 3, 4, 5}; // Using operator< auto result1 = boost::range::equal_range(v, 2); // result1.first points to the first '2', result1.second points after the last '2' // Using a custom predicate (e.g., for descending order) auto result2 = boost::range::equal_range(v, 2, std::greater()); // This would not work as expected because the vector is not sorted in descending order. return 0; } ``` -------------------------------- ### Boost.Range iota Algorithm in C++ Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/algorithms/new/iota The iota algorithm traverses a forward range and assigns values to each element. The value assigned is equivalent to an initial value 'x' plus the distance from the beginning of the range to the current element. It requires a ForwardRange and an Incrementable Value type. The complexity is linear with respect to the distance of the range. ```cpp #include #include int main() { std::vector v = {0, 0, 0, 0}; boost::range::iota(v, 1); // v is now {1, 2, 3, 4} return 0; } ``` -------------------------------- ### Replace count_if with Range Adaptor Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/introduction This snippet shows how to replace the `boost::count_if` algorithm with a more expressive Range Adaptor-based approach. It leverages `boost::size` and `boost::adaptors::filtered` for a concise and efficient solution, avoiding the need for a separate `_if` suffix. ```C++ boost::count_if( rng, pred ); // Alternative using Range Adaptors: boost::size( rng | boost::adaptors::filtered(pred) ); ``` -------------------------------- ### next_permutation Algorithm Prototypes (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/algorithms/permutation/next_permutation Provides the various function signatures for the next_permutation algorithm. It supports ranges with or without a custom comparison predicate, and mutable or constant ranges. ```cpp template bool next_permutation(BidirectionalRange& rng); template bool next_permutation(const BidirectionalRange& rng); template bool next_permutation(BidirectionalRange& rng, Compare pred); template bool next_permutation(const BidirectionalRange& rng, Compare pred); ``` -------------------------------- ### Boost.Range indexed adaptor for C++ Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/reference/indexed The boost::adaptors::indexed adaptor enhances a range to yield elements along with their numeric index. It can optionally take a starting index. The returned elements are of type boost::range::index_value, containing both the original value and its index. The range category is Single Pass Range, demoted to Forward Range if the input is a Bidirectional Range to ensure efficient index calculation. ```cpp #include #include #include #include #include int main(int argc, const char* argv[]) { using namespace boost::assign; using namespace boost::adaptors; std::vector input; input += 10,20,30,40,50,60,70,80,90; for (const auto& element : input | indexed(0)) { std::cout << "Element = " << element.value() << " Index = " << element.index() << std::endl; } return 0; } ``` -------------------------------- ### push_heap - Add Element to Heap (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/algorithms/heap/push_heap The `push_heap` function adds an element to a heap. It assumes that the range `[begin(rng), prior(end(rng)))` is already a heap and the element to be added is `*prior(end(rng))`. It supports both default less-than comparison and a user-defined predicate for ordering. ```C++ template RandomAccessRange& push_heap(RandomAccessRange& rng); template const RandomAccessRange& push_heap(const RandomAccessRange& rng); template RandomAccessRange& push_heap(RandomAccessRange& rng, Compare pred); template const RandomAccessRange& push_heap(const RandomAccessRange& rng, Compare pred); ``` -------------------------------- ### Range Adaptor Alternative to copy_if Algorithm Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/introduction Shows how to achieve the functionality of `boost::copy_if` using a Range Adaptor. The `boost::adaptors::filtered(pred)` adaptor is applied to the range before passing it to the standard `boost::copy` algorithm, demonstrating the orthogonality of adaptors and algorithms. ```C++ boost::copy( rng | boost::adaptors::filtered(pred), out ); ``` -------------------------------- ### Implement Range Concepts via Free-standing Functions and Metafunction Specialization (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/extending/method_2 This C++ code demonstrates how to make a custom 'Pair' type conform to Boost.Range concepts. It involves specializing metafunctions within the 'boost' namespace and defining free-standing range_begin and range_end functions in the same namespace as the custom type. This allows standard Boost.Range algorithms to work with the custom type. ```C++ #include #include // for std::iterator_traits, std::distance() namespace Foo { // // Our sample UDT. A 'Pair' // will work as a range when the stored // elements are iterators. // template< class T > struct Pair { T first, last; }; } // namespace 'Foo' namespace boost { // // Specialize metafunctions. We must include the range.hpp header. // We must open the 'boost' namespace. // template< class T > struct range_mutable_iterator< Foo::Pair > { typedef T type; }; template< class T > struct range_const_iterator< Foo::Pair > { // // Remark: this is defined similar to 'range_iterator' // because the 'Pair' type does not distinguish // between an iterator and a const_iterator. // typedef T type; }; } // namespace 'boost' namespace Foo { // // The required functions. These should be defined in // the same namespace as 'Pair', in this case // in namespace 'Foo'. // template< class T > inline T range_begin( Pair& x ) { return x.first; } template< class T > inline T range_begin( const Pair& x ) { return x.first; } template< class T > inline T range_end( Pair& x ) { return x.last; } template< class T > inline T range_end( const Pair& x ) { return x.last; } } // namespace 'Foo' #include int main(int argc, const char* argv[]) { typedef std::vector::iterator iter; std::vector vec; Foo::Pair pair = { vec.begin(), vec.end() }; const Foo::Pair& cpair = pair; // // Notice that we call 'begin' etc with qualification. // iter i = boost::begin( pair ); iter e = boost::end( pair ); i = boost::begin( cpair ); e = boost::end( cpair ); boost::range_difference< Foo::Pair >::type s = boost::size( pair ); s = boost::size( cpair ); boost::range_reverse_iterator< const Foo::Pair >::type ri = boost::rbegin( cpair ), re = boost::rend( cpair ); return 0; } ``` -------------------------------- ### C++: Define forwarder and pipe operator for range adaptor Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/extending/method_3/method_3_2 Defines a `forwarder2` instance named `replaced` and overloads `operator|` for the `replace_holder`. This enables the use of the pipe syntax (e.g., `my_range | replaced(from_val, to_val)`) to apply the range adaptor. ```cpp static boost::range_detail::forwarder2 replaced = boost::range_detail::forwarder2(); template inline replace_range operator|(SinglePassRange& rng, const replace_holder::type>& f) { return replace_range(rng, f.val1, f.val2); } template inline replace_range operator|(const SinglePassRange& rng, const replace_holder::type>& f) { return replace_range(rng, f.val1, f.val2); } ``` -------------------------------- ### Function join Synopsis - C++ Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/utilities/join This snippet shows the C++ synopsis for the `join` function in Boost.Range. It includes both const and mutable overloads, detailing their template parameters and return types. The function takes two ranges and returns a `joined_range` object. ```cpp template joined_range join(const SinglePassRange1& rng1, const SinglePassRange2& rng2); template joined_range join(SinglePassRange1& rng1, SinglePassRange2& rng2); ``` -------------------------------- ### Filter and reverse map values with Range adaptors Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/introduction Demonstrates chaining multiple Boost Range adaptors together to filter even values from a map and reverse their order before inserting into a target container. This shows the composability of range adapters using the pipe operator, combining map_values, filtered, and reversed adaptors. ```cpp using namespace boost; using namespace boost::adaptors; // Assume that is_even is a predicate that has been implemented elsewhere... push_back(target, my_map | map_values | filtered(is_even()) | reversed); ``` -------------------------------- ### Extract map keys using Boost.Range map_keys (C++) Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/adaptors/reference/map_keys This C++ code snippet demonstrates how to use the boost::adaptors::map_keys to extract keys from a std::map. It requires the Boost.Range library and standard library headers for I/O and containers. The output is a comma-separated list of the map's keys. ```cpp #include #include #include #include #include #include #include int main(int argc, const char* argv[]) { using namespace boost::assign; using namespace boost::adaptors; std::map input; for (int i = 0; i < 10; ++i) input.insert(std::make_pair(i, i * 10)); boost::copy( input | map_keys, std::ostream_iterator(std::cout, ",")); return 0; } ``` -------------------------------- ### Boost copy_backward Algorithm Prototype Source: https://www.boost.org/doc/libs/latest/libs/range/doc/html/range/reference/algorithms/mutating/copy_backward The `copy_backward` algorithm copies elements from a source range in reverse order to an output iterator. It requires bidirectional ranges and output iterators, and performs a linear number of assignments. ```cpp template BidirectionalOutputIterator copy_backward(const BidirectionalRange& source_rng, BidirectionalOutputIterator out_it); ```