### Counting Iterator Example Usage Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/counting_iterator Illustrates practical examples of how to use the counting iterator adaptor in C++ code, showing its application with standard algorithms. ```rst .. include:: counting_iterator_eg.rst ``` -------------------------------- ### Counting Iterator Abstract Example Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/counting_iterator An example demonstrating the abstract usage and concept of the counting iterator, highlighting its role in filling containers with sequences of numbers. ```rst .. include:: counting_iterator_abstract.rst ``` -------------------------------- ### Counting Iterator Example - C++ Source: https://www.boost.org/doc/libs/latest/libs/iterator/example/counting_iterator_example Demonstrates the use of boost::counting_iterator to generate a sequence of integers. It shows how to copy these generated numbers to an output stream and how to use them to fill a vector. ```cpp // (C) Copyright Jeremy Siek 2000-2004. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include #include #include #include #include int main(int, char*[]) { // Example of using counting_iterator std::cout << "counting from 0 to 4:" << std::endl; boost::counting_iterator first(0), last(4); std::copy(first, last, std::ostream_iterator(std::cout, " ")); std::cout << std::endl; // Example of using counting iterator to create an array of pointers. int N = 7; std::vector numbers; typedef std::vector::iterator n_iter; // Fill "numbers" array with [0,N) std::copy( boost::counting_iterator(0) , boost::counting_iterator(N) , std::back_inserter(numbers)); std::vector::iterator> pointers; // Use counting iterator to fill in the array of pointers. // causes an ICE with MSVC6 std::copy(boost::make_counting_iterator(numbers.begin()), boost::make_counting_iterator(numbers.end()), std::back_inserter(pointers)); // Use indirect iterator to print out numbers by accessing // them through the array of pointers. std::cout << "indirectly printing out the numbers from 0 to " << N << std::endl; std::copy(boost::make_indirect_iterator(pointers.begin()), boost::make_indirect_iterator(pointers.end()), std::ostream_iterator(std::cout, " ")); std::cout << std::endl; return boost::exit_success; } ``` -------------------------------- ### Boost Permutation Iterator Example in C++ Source: https://www.boost.org/doc/libs/latest/libs/iterator/example/permutation_iter_example This C++ code demonstrates the functionality of boost::iterator::permutation_iterator. It initializes a vector of integers and a deque of indices, then uses permutation_iterator to create a view of the vector reordered by the indices. The example shows how to copy elements from the permuted range, access elements at specific intervals, and iterate both forwards and backwards. ```cpp #include #include #include #include #include #include #include int main() { using namespace boost; int i = 0; typedef std::vector< int > element_range_type; typedef std::deque< int > index_type; static const int element_range_size = 10; static const int index_size = 4; element_range_type elements( element_range_size ); for(element_range_type::iterator el_it = elements.begin() ; el_it != elements.end() ; ++el_it) *el_it = std::distance(elements.begin(), el_it); index_type indices( index_size ); for(index_type::iterator i_it = indices.begin() ; i_it != indices.end() ; ++i_it ) *i_it = element_range_size - index_size + std::distance(indices.begin(), i_it); std::reverse( indices.begin(), indices.end() ); typedef permutation_iterator< element_range_type::iterator, index_type::iterator > permutation_type; permutation_type begin = make_permutation_iterator( elements.begin(), indices.begin() ); permutation_type it = begin; permutation_type end = make_permutation_iterator( elements.begin(), indices.end() ); std::cout << "The original range is : "; std::copy( elements.begin(), elements.end(), std::ostream_iterator< int >( std::cout, " " ) ); std::cout << "\n"; std::cout << "The reindexing scheme is : "; std::copy( indices.begin(), indices.end(), std::ostream_iterator< int >( std::cout, " " ) ); std::cout << "\n"; std::cout << "The permutated range is : "; std::copy( begin, end, std::ostream_iterator< int >( std::cout, " " ) ); std::cout << "\n"; std::cout << "Elements at even indices in the permutation : "; it = begin; for(i = 0; i < index_size / 2 ; ++i, it+=2 ) std::cout << *it << " "; std::cout << "\n"; std::cout << "Permutation backwards : "; it = begin + (index_size); assert( it != begin ); for( ; it-- != begin ; ) std::cout << *it << " "; std::cout << "\n"; std::cout << "Iterate backward with stride 2 : "; it = begin + (index_size - 1); for(i = 0 ; i < index_size / 2 ; ++i, it-=2 ) std::cout << *it << " "; std::cout << "\n"; return boost::exit_success; } ``` -------------------------------- ### Boost transform_iterator Example: Multiplying by 2 Source: https://www.boost.org/doc/libs/latest/libs/iterator/example/transform_iterator_example An example demonstrating the use of Boost's transform_iterator to multiply each element of an integer array by 2. It utilizes a custom binder1st and std::multiplies to achieve the transformation during iteration. The output prints the modified array elements. ```C++ int main(int, char*[]) { // This is a simple example of using the transform_iterators class to // generate iterators that multiply the value returned by dereferencing // the iterator. In this case we are multiplying by 2. // Would be cooler to use lambda library in this example. int x[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; const int N = sizeof(x)/sizeof(int); typedef boost::binder1st< std::multiplies > Function; typedef boost::transform_iterator doubling_iterator; doubling_iterator i(x, boost::bind1st(std::multiplies(), 2)), i_end(x + N, boost::bind1st(std::multiplies(), 2)); std::cout << "multiplying the array by 2:" << std::endl; while (i != i_end) std::cout << *i++ << " "; std::cout << std::endl; // ... rest of the main function ... return 0; } ``` -------------------------------- ### Example: Using Generator Iterator with a Custom Generator Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/generator_iterator This C++ example demonstrates how to use `boost::iterators::make_generator_iterator` to transform a custom generator class (`my_generator`) into an input iterator. The program then iterates through the generated values using a standard for loop and prints them. ```cpp #include #include class my_generator { public: using result_type = int; my_generator() : state(0) { } result_type operator()() { return ++state; } private: int state; }; int main() { my_generator gen; auto it = boost::iterators::make_generator_iterator(gen); for(int i = 0; i < 10; ++i, ++it) std::cout << *it << std::endl; } ``` -------------------------------- ### Node Iterator Interoperability Check (C++) Source: https://www.boost.org/doc/libs/latest/libs/iterator/example/node_iterator3 Demonstrates the interoperability between node_iterator and node_const_iterator in C++. It verifies equality and inequality assertions between these iterator types, ensuring correct behavior when comparing different constness levels. ```cpp #include "node_iterator3.hpp" #include #include #include #include #include #include int main() { #if defined(BOOST_NO_CXX11_SMART_PTR) std::auto_ptr > nodes(new node(42)); #else std::unique_ptr > nodes(new node(42)); #endif nodes->append(new node(" is greater than ")); nodes->append(new node(13)); // Check interoperability assert(node_iterator(nodes.get()) == node_const_iterator(nodes.get())); assert(node_const_iterator(nodes.get()) == node_iterator(nodes.get())); assert(node_iterator(nodes.get()) != node_const_iterator()); assert(node_const_iterator(nodes.get()) != node_iterator()); std::copy( node_iterator(nodes.get()), node_iterator() , std::ostream_iterator(std::cout, " ") ); std::cout << std::endl; std::for_each( node_iterator(nodes.get()), node_iterator() , boost::mem_fn(&node_base::double_me) ); std::copy( node_const_iterator(nodes.get()), node_const_iterator() , std::ostream_iterator(std::cout, "/") ); std::cout << std::endl; return 0; } ``` -------------------------------- ### Boost transform_iterator Example: Adding 4 Source: https://www.boost.org/doc/libs/latest/libs/iterator/example/transform_iterator_example This C++ code snippet illustrates using Boost's transform_iterator with std::copy and std::ostream_iterator to add 4 to each element of an integer array and print the results to standard output. It employs boost::make_transform_iterator and boost::bind1st with std::plus. ```C++ int main(int, char*[]) { // ... previous code ... std::cout << "adding 4 to each element in the array:" << std::endl; std::copy(boost::make_transform_iterator(x, boost::bind1st(std::plus(), 4)), boost::make_transform_iterator(x + N, boost::bind1st(std::plus(), 4)), std::ostream_iterator(std::cout, " ")); std::cout << std::endl; return 0; } ``` -------------------------------- ### C++ Node Iterator Usage for Copying and Modification Source: https://www.boost.org/doc/libs/latest/libs/iterator/example/node_iterator1 This C++ code snippet demonstrates the use of custom node iterators with Boost. It shows how to initialize a node list, copy its elements to an output stream using `std::copy` and `std::ostream_iterator`, and then modify elements using `std::for_each` and `std::mem_fun_ref`. The code also handles differences in smart pointer availability between C++11 and earlier standards. ```cpp // Copyright David Abrahams 2004. Use, modification and distribution is // subject to the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include "node_iterator1.hpp" #include #include #include #include #include int main() { #if defined(BOOST_NO_CXX11_SMART_PTR) std::auto_ptr > nodes(new node(42)); #else std::unique_ptr > nodes(new node(42)); #endif nodes->append(new node(" is greater than ")); nodes->append(new node(13)); std::copy( node_iterator(nodes.get()), node_iterator() , std::ostream_iterator(std::cout, " ") ); std::cout << std::endl; std::for_each( node_iterator(nodes.get()), node_iterator() , std::mem_fun_ref(&node_base::double_me) ); std::copy( node_iterator(nodes.get()), node_iterator() , std::ostream_iterator(std::cout, "/") ); std::cout << std::endl; } ``` -------------------------------- ### C++: Reverse and Double-Reverse Sequence with boost::reverse_iterator Source: https://www.boost.org/doc/libs/latest/libs/iterator/example/reverse_iterator_example This C++ code demonstrates how to use boost::reverse_iterator to print a character sequence in reverse order. It initializes a character array, creates forward and reverse iterators, and then uses std::copy to print the reversed sequence. A second std::copy operation with double-reversed iterators shows how to restore the original order. This snippet requires the Boost Iterator library. ```C++ // (C) Copyright Jeremy Siek 2000-2004. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include #include int main(int, char*[]) { char letters_[] = "hello world!"; const int N = sizeof(letters_)/sizeof(char) - 1; typedef char* base_iterator; base_iterator letters(letters_); std::cout << "original sequence of letters:\t\t\t\t" << letters_ << std::endl; // Use reverse_iterator to print a sequence of letters in reverse // order. boost::reverse_iterator reverse_letters_first(letters + N), reverse_letters_last(letters); std::cout << "sequence in reverse order:\t\t\t"; std::copy(reverse_letters_first, reverse_letters_last, std::ostream_iterator(std::cout)); std::cout << std::endl; std::cout << "sequence in double-reversed (normal) order:\t"; std::copy(boost::make_reverse_iterator(reverse_letters_last), boost::make_reverse_iterator(reverse_letters_first), std::ostream_iterator(std::cout)); std::cout << std::endl; return boost::exit_success; } ``` -------------------------------- ### Filter Numbers Greater Than -2 using boost::bind Source: https://www.boost.org/doc/libs/latest/libs/iterator/example/filter_iterator_example This snippet demonstrates filtering an array of integers to include only those greater than -2. It uses `boost::bind` with `std::greater` and `boost::placeholders::_1` to create the predicate dynamically. The filtered elements are output to `std::cout`. ```cpp // (C) Copyright Jeremy Siek 1999-2004. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include #include #include #include // for exit_success int main() { int numbers_[] = { 0, -1, 4, -3, 5, 8, -2 }; const int N = sizeof(numbers_)/sizeof(int); typedef int* base_iterator; base_iterator numbers(numbers_); // Another example using make_filter_iterator() std::copy( boost::make_filter_iterator( boost::bind(std::greater(), boost::placeholders::_1, -2) , numbers, numbers + N) , boost::make_filter_iterator( boost::bind(std::greater(), boost::placeholders::_1, -2) , numbers + N, numbers + N) , std::ostream_iterator(std::cout, " ") ); std::cout << std::endl; return boost::exit_success; } ``` -------------------------------- ### Function Input Iterator Example: Bounded Range Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/function_input_iterator Demonstrates using function_input_iterator to create a bounded range of random integers. The 'generator' struct provides the random number generation, and the state '0' and '10' define the bounds. ```c++ struct generator { typedef int result_type; generator() { srand(time(0)); } result_type operator() () const { return rand(); } }; int main(int argc, char * argv[]) { generator f; copy( make_function_input_iterator(f, 0), make_function_input_iterator(f, 10), ostream_iterator(cout, " ") ); return 0; } ``` -------------------------------- ### C++ Node Iterator Implementation using Boost Iterator Facade Source: https://www.boost.org/doc/libs/latest/libs/iterator/example/node_iterator2 Implements a forward iterator for a node-based structure using `boost::iterator_facade`. This class provides basic iterator functionalities like dereferencing and incrementing, suitable for traversing linked lists or similar node structures. It handles conversions from iterators of compatible node types. ```cpp // Copyright David Abrahams 2004. Use, modification and distribution is // subject to the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef NODE_ITERATOR2_DWA2004110_HPP # define NODE_ITERATOR2_DWA2004110_HPP # include "node.hpp" # include # include template class node_iter : public boost::iterator_facade< node_iter , Value , boost::forward_traversal_tag > { private: struct enabler {}; // a private type avoids misuse public: node_iter() : m_node(0) {} explicit node_iter(Value* p) : m_node(p) {} template node_iter( node_iter const& other , typename std::enable_if< std::is_convertible::value , enabler >::type = enabler() ) : m_node(other.m_node) {} template bool equal(node_iter const& other) const { return this->m_node == other.m_node; } void increment() { m_node = m_node->next(); } Value& dereference() const { return *m_node; } # ifdef BOOST_NO_MEMBER_TEMPLATE_FRIENDS public: # else private: template friend class node_iter; # endif Value* m_node; }; typedef node_iter node_iterator; typedef node_iter node_const_iterator; #endif // NODE_ITERATOR2_DWA2004110_HPP ``` -------------------------------- ### Function Input Iterator Example: Unbounded Range Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/function_input_iterator Illustrates creating an unbounded (endless) stream of random numbers using function_input_iterator with the boost::infinite helper class. This avoids creating large in-memory collections. ```c++ copy( make_function_input_iterator(f,infinite()), make_function_input_iterator(f,infinite()), ostream_iterator(cout, " ") ); ``` -------------------------------- ### Indirect Iterator with Pointers to Characters (C++) Source: https://www.boost.org/doc/libs/latest/libs/iterator/example/indirect_iterator_example This snippet demonstrates the primary use of boost::indirect_iterator by creating an iterator that dereferences pointers to characters. It iterates over an array of character pointers and copies the pointed-to characters to an output stream, separated by commas. Dependencies include , , , , , and . ```C++ #include #include #include #include #include #include #include #include int main(int, char*[]) { char characters[] = "abcdefg"; const int N = sizeof(characters)/sizeof(char) - 1; // -1 since characters has a null char char* pointers_to_chars[N]; // at the end. for (int i = 0; i < N; ++i) pointers_to_chars[i] = &characters[i]; // Example of using indirect_iterator boost::indirect_iterator indirect_first(pointers_to_chars), indirect_last(pointers_to_chars + N); std::copy(indirect_first, indirect_last, std::ostream_iterator(std::cout, ",")); std::cout << std::endl; // Example of making mutable and constant indirect iterators char mutable_characters[N]; char* pointers_to_mutable_chars[N]; for (int j = 0; j < N; ++j) pointers_to_mutable_chars[j] = &mutable_characters[j]; boost::indirect_iterator mutable_indirect_first(pointers_to_mutable_chars), mutable_indirect_last(pointers_to_mutable_chars + N); boost::indirect_iterator const_indirect_first(pointers_to_chars), const_indirect_last(pointers_to_chars + N); std::transform(const_indirect_first, const_indirect_last, mutable_indirect_first, boost::bind(std::plus(), 1, boost::placeholders::_1)); std::copy(mutable_indirect_first, mutable_indirect_last, std::ostream_iterator(std::cout, ",")); std::cout << std::endl; // Example of using make_indirect_iterator() std::copy(boost::make_indirect_iterator(pointers_to_chars), boost::make_indirect_iterator(pointers_to_chars + N), std::ostream_iterator(std::cout, ",")); std::cout << std::endl; return 0; } ``` -------------------------------- ### Counting Iterator Class Template Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/facade-and-adaptor Defines the `counting_iterator` class template, which generates a sequence of values, typically integers, starting from a specified value and incrementing. It's useful for creating iterator ranges without backing containers. ```C++ template < class Incrementable, class CategoryOrTraversal = use_default, class Difference = use_default > class counting_iterator; ``` -------------------------------- ### Filter Positive Numbers with Functor Source: https://www.boost.org/doc/libs/latest/libs/iterator/example/filter_iterator_example This snippet filters an array of integers to include only positive numbers. It utilizes a custom functor `is_positive_number` as the predicate. The output is printed to `std::cout`, separated by spaces. ```cpp // (C) Copyright Jeremy Siek 1999-2004. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include #include #include #include // for exit_success struct is_positive_number { bool operator()(int x) { return 0 < x; } }; int main() { int numbers_[] = { 0, -1, 4, -3, 5, 8, -2 }; const int N = sizeof(numbers_)/sizeof(int); typedef int* base_iterator; base_iterator numbers(numbers_); // Example using make_filter_iterator() std::copy(boost::make_filter_iterator(numbers, numbers + N), boost::make_filter_iterator(numbers + N, numbers + N), std::ostream_iterator(std::cout, " ")); std::cout << std::endl; // Example using filter_iterator typedef boost::filter_iterator FilterIter; is_positive_number predicate; FilterIter filter_iter_first(predicate, numbers, numbers + N); FilterIter filter_iter_last(predicate, numbers + N, numbers + N); std::copy(filter_iter_first, filter_iter_last, std::ostream_iterator(std::cout, " ")); std::cout << std::endl; return boost::exit_success; } ``` -------------------------------- ### Boost.GeneratorIterator Synopsis Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/generator_iterator This snippet shows the basic synopsis for the `generator_iterator` adaptor, including the namespace and template class/function signatures. It defines how to construct and use a generator iterator. ```cpp namespace boost { namespace iterators { template class generator_iterator_generator; template typename generator_iterator_generator::type make_generator_iterator(Generator & gen); } } ``` -------------------------------- ### Enable If Convertible Utility Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/facade-and-adaptor The `enable_if_convertible` utility is a metafunction used with SFINAE to conditionally enable or disable constructors and functions based on the implicit convertibility between types. It ensures that adaptors are only used when type conversions are safe. ```C++ template struct enable_if_convertible : enable_if_convertible_impl::value> {}; template enable_if_convertible_impl {}; template <> enable_if_convertible_impl { struct type; }; ``` -------------------------------- ### Make Counting Iterator Synopsis Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/counting_iterator Details the C++ synopsis for the make_counting_iterator function, a utility for easily creating counting iterators. ```rst .. include:: make_counting_iterator.rst ``` -------------------------------- ### Counting Iterator Synopsis Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/counting_iterator Provides the C++ synopsis for the counting_iterator class, outlining its interface and template parameters. ```rst .. include:: counting_iterator_ref.rst ``` -------------------------------- ### Boost Custom binder1st Implementation Source: https://www.boost.org/doc/libs/latest/libs/iterator/example/transform_iterator_example A C++ implementation of a binder1st class that addresses the default constructor limitation of std::binder1st, allowing its use with transform iterators. It takes an operation and a value, creating a callable object that applies the operation with the stored value and a provided argument. ```C++ // (C) Copyright Jeremy Siek 2000-2004. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include // What a bummer. We can't use std::binder1st with transform iterator // because it does not have a default constructor. Here's a version // that does. namespace boost { template class binder1st { public: typedef typename Operation::result_type result_type; typedef typename Operation::second_argument_type argument_type; protected: Operation op; typename Operation::first_argument_type value; public: binder1st() { } // this had to be added! binder1st(const Operation& x, const typename Operation::first_argument_type& y) : op(x), value(y) {} typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const { return op(value, x); } }; template inline binder1st bind1st(const Operation& op, const T& x) { typedef typename Operation::first_argument_type arg1_type; return binder1st(op, arg1_type(x)); } } // namespace boost ``` -------------------------------- ### Iterator Assignment and Construction (C++) Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/new-iter-concepts Illustrates how to assign one iterator to another and construct a new iterator from an existing one. These operations ensure that the new iterator is equivalent to the source iterator, maintaining the postcondition y == x. ```C++ // Assignment y = x; // post: y == x // Construction Y(x); // post: Y(x) == x ``` -------------------------------- ### Use Default Sentinel Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/facade-and-adaptor The use_default struct is a placeholder or tag used to indicate that default behavior should be employed, often in contexts where specific customization might otherwise be required. It simplifies the interface for certain iterator operations. ```c++ struct use_default {}; ``` -------------------------------- ### Random Access Traversal: Iterator Requirements and Expressions Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/RandomAccessTraversal Details the expressions, return types, and operational semantics for iterators that model the Random Access Traversal concept. This includes addition, subtraction, and element access operations. ```C++ r += n // Return Type: X& // Operational Semantics: { // Distance m = n; // if (m >= 0) // while (m--) // ++r; // else // while (m++) // --r; // return r; // } a + n, n + a // Return Type: X // Operational Semantics: { X tmp = a; return tmp += n; } r -= n // Return Type: X& // Operational Semantics: return r += -n a - n // Return Type: X // Operational Semantics: { X tmp = a; return tmp -= n; } b - a // Return Type: Distance // Operational Semantics: a < b ? distance(a,b) : -distance(b,a) // Precondition: there exists a value n of Distance such that a + n == b. b == a + (b - a). a[n] // Return Type: convertible to T // Operational Semantics: *(a + n) // Precondition: a is a *Readable Iterator* ``` -------------------------------- ### Iterator Arithmetic Operations (C++) Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/new-iter-concepts Demonstrates common arithmetic operations for iterators, including subtraction to find distance, and addition/offsetting for accessing elements. These operations assume specific iterator categories and preconditions for valid execution. ```C++ tmp = a; return tmp -= n; // For distance a < b ? distance(a, b) : -distance(b, a); // Accessing element at offset n *(a + n); // Assigning value v at offset n *(a + n) = v; ``` -------------------------------- ### Specializing boost::pointee for custom smart pointers Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/pointee This code snippet demonstrates how to specialize the boost::pointee template for a custom smart pointer type. This allows the 'pointee' utility to correctly deduce the referent type for user-defined smart pointer classes, ensuring compatibility with generic code that relies on this deduction. ```C++ namespace boost { template struct pointee > { typedef T type; }; } ``` -------------------------------- ### Function Output Iterator Class Template Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/facade-and-adaptor Defines the `function_output_iterator` class template, which applies a unary function to each value written to it. It acts as an output iterator that performs an action on each element assigned. ```C++ template class function_output_iterator; ``` -------------------------------- ### Iterator Facade Class Template Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/facade-and-adaptor Defines the `iterator_facade` class template, a foundation for creating custom iterators with minimal boilerplate code. It simplifies the implementation of iterator concepts by requiring only a few member functions to be defined by the derived class. ```C++ template < class Value, class CategoryOrTraversal, class Reference = Value&, class Difference = ptrdiff_t > class iterator_facade; ``` -------------------------------- ### Make Generator Iterator Function Template Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/generator_iterator The `make_generator_iterator` function provides a convenient factory method to create instances of generator iterators. It simplifies the process by abstracting away the explicit iterator type construction. ```cpp template typename generator_iterator_generator::type make_generator_iterator(Generator & gen); ``` -------------------------------- ### Iterator Equality Comparison (C++) Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/new-iter-concepts Shows the equality (==) and inequality (!=) comparison operations for iterators. These operators are defined to be equivalence relations over their domain, ensuring consistent comparison results. ```C++ // Equality check x == y; // convertible to bool y == x; // convertible to bool // Inequality check (implied by equality) !(x == y); ``` -------------------------------- ### Iterator Archetype Synopsis and Access Categories Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/iterator_archetypes Defines the iterator_archetype class structure and the standard access category tags used to specify iterator properties. These tags determine the read/write capabilities and mutability of the iterator. ```cpp namespace iterator_archetypes { // Access categories typedef /*implementation defined*/ readable_iterator_t; typedef /*implementation defined*/ writable_iterator_t; typedef /*implementation defined*/ readable_writable_iterator_t; typedef /*implementation defined*/ readable_lvalue_iterator_t; typedef /*implementation defined*/ writable_lvalue_iterator_t; } template < class Value , class AccessCategory , class TraversalCategory > class iterator_archetype { typedef /* see below */ value_type; typedef /* see below */ reference; typedef /* see below */ pointer; typedef /* see below */ difference_type; typedef /* see below */ iterator_category; }; // Access Category Tags readable_iterator_t := Readable Iterator writable_iterator_t := Writeable Iterator readable_writable_iterator_t := Readable Iterator & Writeable Iterator & Swappable Iterator readable_lvalue_iterator_t := Readable Iterator & Lvalue Iterator writeable_lvalue_iterator_t := Readable Iterator & Writeable Iterator & Swappable Iterator & Lvalue Iterator ``` -------------------------------- ### Iterator Comparison Operators Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/new-iter-concepts Defines the requirements for comparison operators (<, >, <=, >=) for iterators, ensuring they form a total ordering relation. Applicable to Random Access Traversal Iterators. ```text +-----------+-----------------------+---------------------+--------------------------------------+ |Expression |Return Type |Operational Semantics|Assertion/ Precondition | +===========+=======================+=====================+======================================+ |``x < y`` |convertible to ``bool``|``y - x > 0`` |``<`` is a total ordering relation | +-----------+-----------------------+---------------------+--------------------------------------+ |``y < x`` |convertible to ``bool``|``x - y > 0`` |``<`` is a total ordering relation | +-----------+-----------------------+---------------------+--------------------------------------+ |``x > y`` |convertible to ``bool``|``y < x`` |``>`` is a total ordering relation | +-----------+-----------------------+---------------------+--------------------------------------+ |``y > x`` |convertible to ``bool``|``x < y`` |``>`` is a total ordering relation | +-----------+-----------------------+---------------------+--------------------------------------+ |``x >= y`` |convertible to ``bool``|``!(x < y)`` | | +-----------+-----------------------+---------------------+--------------------------------------+ |``y >= x`` |convertible to ``bool``|``!(y < x)`` | | +-----------+-----------------------+---------------------+--------------------------------------+ |``x <= y`` |convertible to ``bool``|``!(x > y)`` | | +-----------+-----------------------+---------------------+--------------------------------------+ |``y <= x`` |convertible to ``bool``|``!(y > x)`` | | +-----------+-----------------------+---------------------+--------------------------------------+ ``` -------------------------------- ### Function Input Iterator Synopsis Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/function_input_iterator Defines the function_input_iterator class template and the make_function_input_iterator helper function. It also declares the 'infinite' struct for creating unbounded iterators. ```c++ namespace { template class function_input_iterator; template typename function_input_iterator make_function_input_iterator(Function & f, State s); struct infinite; } ``` -------------------------------- ### Reverse Iterator Class Template Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/facade-and-adaptor Defines the `reverse_iterator` class template, which adapts an existing iterator to traverse a sequence in reverse order. It's a common utility for working with bidirectional and random access iterators. ```C++ template class reverse_iterator; ``` -------------------------------- ### Transform Iterator Adaptor Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/facade-and-adaptor The transform_iterator applies a user-specified function during dereferencing. It wraps an underlying iterator and applies a given function to the result of dereferencing the base iterator. This allows for on-the-fly transformation of iterated values. ```c++ template class transform_iterator : public iterator_adaptor > { public: transform_iterator(); transform_iterator(Iterator iter, Functor func); typename std::iterator_traits::reference operator*() const; private: Functor fun; Iterator iter_; }; template transform_iterator make_transform_iterator(Iterator iter, Functor func) { return transform_iterator(iter, func); } ``` -------------------------------- ### Random Access Traversal Iterator: a + n and n + a Operations Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/new-iter-concepts This C++ code snippet shows the operational semantics for the `a + n` and `n + a` expressions, which are part of the Random Access Traversal Iterator requirements. These expressions return a new iterator that is advanced by `n` positions without modifying the original iterator `a`. This is achieved by creating a temporary copy and applying the `+=` operation. ```cpp { X tmp = a; return tmp += n; } ``` -------------------------------- ### Iterator Adaptor Class Template Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/facade-and-adaptor Defines the `iterator_adaptor` class template, which wraps an existing iterator and modifies its behavior. It's useful for creating new iterators based on existing ones with added functionality or type transformations. ```C++ template < class Derived, class Base, class Value = use_default, class CategoryOrTraversal = use_default, class Reference = use_default, class Difference = use_default > class iterator_adaptor; ``` -------------------------------- ### Iterator Concepts Template Class Declarations C++ Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/iterator_concepts Complete namespace and template class declarations for iterator access and traversal concepts in Boost. These concept checking classes enable compile-time verification of iterator requirements and provide better error messages when type requirements are not met. The templates are parameterized on the Iterator type and optional ValueType. ```C++ namespace boost_concepts { // Iterator Access Concepts template class ReadableIteratorConcept; template < typename Iterator , typename ValueType = std::iterator_traits::value_type > class WritableIteratorConcept; template class SwappableIteratorConcept; template class LvalueIteratorConcept; // Iterator Traversal Concepts template class IncrementableIteratorConcept; template class SinglePassIteratorConcept; template class ForwardTraversalConcept; template class BidirectionalTraversalConcept; template class RandomAccessTraversalConcept; // Interoperability template class InteroperableIteratorConcept; } ``` -------------------------------- ### Iterator Less Than Comparison (a < b) Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/RandomAccessTraversal This operation compares two iterators `a` and `b` to determine if `a` precedes `b` in the sequence. It returns a boolean value and requires that the `<` operator is a total ordering relation. The equivalent expression is `b - a > 0`. ```cpp a < b; ``` -------------------------------- ### Iterator Adaptor Class Template Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/facade-and-adaptor The iterator_adaptor class template serves as a base class for constructing new iterators. It provides default behavior by forwarding operations to an underlying iterator, allowing derived classes to selectively override specific features. This is useful for creating custom iterator functionalities. ```c++ template < class Derived , class Value , class CategoryOrTraversal , class DifferenceType = typename std::iterator_traits::difference_type , class Pointer = typename std::iterator_traits::pointer , class Reference = typename std::iterator_traits::reference > class iterator_adaptor { public: // types typedef typename std::iterator_traits::iterator_category iterator_category; typedef Value value_type; typedef DifferenceType difference_type; typedef Pointer pointer; typedef Reference reference; // constructs iterator_adaptor(); explicit iterator_adaptor(Derived const&); template iterator_adaptor(iterator_ adaptor const&); // access reference operator*() const; pointer operator->() const; // increment/decrement iterator_adaptor& operator++(); iterator_adaptor operator++(int); iterator_adaptor& operator--(); iterator_adaptor operator--(int); // compound assignment iterator_adaptor& operator+=(difference_type n); iterator_adaptor& operator-=(difference_type n); // compound increment/decrement iterator_adaptor& operator+=(difference_type n); iterator_adaptor& operator-=(difference_type n); // conversion operator Derived() const; protected: Derived& derived() { return static_cast(*this); } Derived const& derived() const { return static_cast(*this); } private: // implementation detail Derived m_derived; }; ``` -------------------------------- ### Iterator Core Access (Implementation Detail) Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/facade-and-adaptor The iterator_core_access struct is an implementation detail used for accessing the core components of an iterator, particularly within the context of iterator adaptors. It is not intended for direct user manipulation. ```c++ struct iterator_core_access { template static typename std::iterator_traits::reference dereference(Iter& i) { return *i; } template static void increment(Iter& i) { ++i; } template static void decrement(Iter& i) { --i; } template static bool equal(Iter const& i, Iter const& j) { return i == j; } template static typename std::iterator_traits::difference_type distance(Iter const& i, Iter const& j) { return j - i; } template static void advance(Iter& i, typename std::iterator_traits::difference_type n) { i += n; } template static Iter postincrement(Iter& i) { Iter temp = i; ++i; return temp; } template static Iter postdecrement(Iter& i) { Iter temp = i; --i; return temp; } }; ``` -------------------------------- ### Reverse Iterator Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/facade-and-adaptor The reverse_iterator inverts the direction of traversal for a base iterator. It allows for iterating from the end to the beginning of a sequence. This implementation ensures interoperability between constant and mutable reverse iterators, addressing issues found in C++98. ```c++ template class reverse_iterator : public iterator_adaptor > { public: reverse_iterator(); explicit reverse_iterator(Iterator iter); template reverse_iterator(reverse_iterator const& other); typename std::iterator_traits::reference operator*() const; reverse_iterator& operator++(); reverse_iterator operator++(int); reverse_iterator& operator--(); reverse_iterator operator--(int); private: Iterator iter_; }; ``` -------------------------------- ### Iterator Inequality Check Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/new-iter-concepts Defines the condition for iterator inequality (x != y) being convertible to bool. It must be the logical negation of equality (x == y). ```text +-----------+-----------------------+---------------------------------------------------+ |``x != y`` |convertible to ``bool``|``bool(a==b) != bool(a!=b)`` over its domain. | +-----------+-----------------------+---------------------------------------------------+ |``y != x`` |convertible to ``bool``|``bool(a==b) != bool(a!=b)`` over its domain. | +-----------+-----------------------+---------------------------------------------------+ ``` -------------------------------- ### Iterator Traversal Tags Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/new-iter-concepts Defines tags for different iterator traversal categories, forming a hierarchy from incrementable to random access. These tags are used to classify iterator capabilities. ```cpp // lib.iterator.traits, traits and tags template struct is_readable_iterator; template struct iterator_traversal; struct incrementable_traversal_tag { }; struct single_pass_traversal_tag : incrementable_traversal_tag { }; struct forward_traversal_tag : single_pass_traversal_tag { }; struct bidirectional_traversal_tag : forward_traversal_tag { }; struct random_access_traversal_tag : bidirectional_traversal_tag { }; ``` -------------------------------- ### Iterator Comparison Operations (C++) Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/new-iter-concepts Defines the comparison operators (<, >, <=, >=) for iterators, indicating their ordering relationship. These comparisons rely on the underlying difference type and total ordering relation. ```C++ // Check if a is less than b a < b // equivalent to: b - a > 0 // Check if a is greater than b a > b // equivalent to: b < a // Check if a is greater than or equal to b a >= b // equivalent to: !(a < b) // Check if a is less than or equal to b a <= b // equivalent to: !(a > b) ``` -------------------------------- ### Iterator Less Than or Equal To Comparison (a <= b) Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/RandomAccessTraversal This operation compares two iterators `a` and `b` to determine if `a` is at or before `b` in the sequence. It returns a boolean value. The equivalent expression is `!(a > b)`. ```cpp a <= b; ``` -------------------------------- ### Generator Iterator Generator Class Template Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/generator_iterator The `generator_iterator_generator` is a helper class template used internally to deduce the resulting generator iterator type. It takes the `Generator` type as a template parameter. ```cpp template class generator_iterator_generator { public: using type = _unspecified_; // the resulting generator iterator type }; ``` -------------------------------- ### Transform Iterator Class Template Source: https://www.boost.org/doc/libs/latest/libs/iterator/doc/facade-and-adaptor Defines the `transform_iterator` class template, which applies a unary function to the elements of a sequence as they are iterated over. It allows for on-the-fly transformation of iterator values without modifying the underlying container. ```C++ template < class UnaryFunction, class Iterator, class Reference = use_default, class Value = use_default > class transform_iterator; ```