### Boost.Foreach Hello World Example Source: https://www.boost.org/doc/libs/latest/libs/foreach/index A basic example demonstrating the use of BOOST_FOREACH to iterate over a std::string and print its characters. This showcases the simplified loop syntax compared to traditional C++ iteration methods. ```cpp #include #include #include int main() { std::string hello( "Hello, world!" ); BOOST_FOREACH( char ch, hello ) { std::cout << ch; } return 0; } ``` -------------------------------- ### Modifying Sequence Elements via Boost.Foreach Reference Source: https://www.boost.org/doc/libs/latest/libs/foreach/index Demonstrates iterating over an array by reference using BOOST_FOREACH, allowing modification of the underlying array elements. The example shows incrementing each element. ```cpp short array_short[] = { 1, 2, 3 }; BOOST_FOREACH( short & i, array_short ) { ++i; } // array_short contains {2,3,4} here ``` -------------------------------- ### Reverse Iteration with Boost.Reverse_Foreach Source: https://www.boost.org/doc/libs/latest/libs/foreach/index Demonstrates iterating over a std::list in reverse order using the BOOST_REVERSE_FOREACH macro. This provides a convenient way to process sequences from end to beginning. ```cpp std::list list_int( /*...*/ ); BOOST_REVERSE_FOREACH( int i, list_int ) { // do something with i } ``` -------------------------------- ### Iterating Over STL Containers with Boost.Foreach Source: https://www.boost.org/doc/libs/latest/libs/foreach/index Demonstrates iterating over a std::list using BOOST_FOREACH. This is a common use case for iterating through standard C++ containers. ```cpp std::list list_int( /*...*/ ); BOOST_FOREACH( int i, list_int ) { // do something with i } ``` -------------------------------- ### Define Custom Macros for BOOST_FOREACH Source: https://www.boost.org/doc/libs/latest/libs/foreach/index This snippet demonstrates how to create custom preprocessor macros, such as `foreach_` and `foreach_r_`, that alias the standard `BOOST_FOREACH` and `BOOST_REVERSE_FOREACH` macros. This is useful for shortening long identifiers or adhering to different naming conventions. Ensure the chosen identifiers do not conflict with existing names in your project. ```c++ #define foreach_ BOOST_FOREACH #define foreach_r_ BOOST_REVERSE_FOREACH ``` -------------------------------- ### Nested Boost.Foreach Loops for Nested Containers Source: https://www.boost.org/doc/libs/latest/libs/foreach/index Illustrates using nested BOOST_FOREACH loops to iterate over a vector of vectors (a matrix). Braces around the loop body are shown to be optional for single statements. ```cpp std::vector > matrix_int; BOOST_FOREACH( std::vector & row, matrix_int ) BOOST_FOREACH( int & i, row ) ++i; ``` -------------------------------- ### Boost.Foreach Loop with Predeclared Variable and Control Flow Source: https://www.boost.org/doc/libs/latest/libs/foreach/index Shows how to predeclare the loop variable with BOOST_FOREACH and use standard control flow statements like 'break', 'continue', and 'return' within the loop body. ```cpp std::deque deque_int( /*...*/ ); int i = 0; BOOST_FOREACH( i, deque_int ) { if( i == 0 ) return; if( i == 1 ) continue; if( i == 2 ) break; } ``` -------------------------------- ### Boost.Foreach Iteration Over Rvalue Sequences Source: https://www.boost.org/doc/libs/latest/libs/foreach/index Shows how BOOST_FOREACH can iterate over the result of a function that returns a sequence by value (an rvalue). It notes that the function is called exactly once. ```cpp extern std::vector get_vector_float(); BOOST_FOREACH( float f, get_vector_float() ) { // Note: get_vector_float() will be called exactly once } ``` -------------------------------- ### Workaround for 'foreach' Namespace Conflict with BOOST_FOREACH Source: https://www.boost.org/doc/libs/latest/libs/foreach/index This code provides a workaround to mitigate naming conflicts when using 'foreach' as a macro alias for `BOOST_FOREACH`, particularly when the `boost::foreach` namespace is involved. It creates an alias within the `boost` namespace and then defines the `foreach` macro. This approach helps resolve some, but not all, potential conflicts. It is generally recommended to use a unique identifier other than 'foreach'. ```c++ #include namespace boost { // Suggested work-around for https://svn.boost.org/trac/boost/ticket/6131 namespace BOOST_FOREACH = foreach; } #define foreach BOOST_FOREACH ``` -------------------------------- ### Boost.Foreach Iteration Over Arrays with Covariance Source: https://www.boost.org/doc/libs/latest/libs/foreach/index Illustrates iterating over a short array using BOOST_FOREACH, where the loop variable (int) has a different type than the array elements (short). This shows implicit type conversion during iteration. ```cpp short array_short[] = {1,2,3}; BOOST_FOREACH( int i, array_short ) { // The short was implicitly converted to an int } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.