C++ boost::foreach














































C++ boost::foreach



Description:
We use for or for_each to iterate over a sequence.
 BOOST_FOREACH is just such a construct for C++.
It iterates over sequences so we don't have to iterate over the loop and create variables for
iteration.

BOOST_FOREACH iterates over sequences. But what is a sequence? Since BOOST_FOREACH
is built on top of Boost.Range, it automatically supports those types which Boost.Range
recognizes as sequences. Specifically, BOOST_FOREACH works with the types that satisfy the
Single Pass Range Concept. For example, we can use BOOST_FOREACH with:

1) STL containers
2) arrays
3) Null-terminated strings (char and wchar_t)
4) std::pair of iterators


Syntax:
BOOST_FOREACH(variable,name of container)
{
do something with the container elements
}

Note:
This article is just for explaining the working and use of boost::foreach .

Advantages:
1) BOOST_FOREACH is designed for ease-of-use and efficiency.
2) It makes no virtual function calls.
3) The use of BOOST_FOREACH results in near-optimal code generation.








// Include all headers. If we don't include bits/stdc++.h, we
// will have to include the commented headers that is iostream.
#include<bits/stdc++.h>
#include <boost/foreach.hpp>

//#include <iostream>


using namespace std;

int main()
{
// declaring a string with variable name hello.
string hello( "Hello, world!" );
// using BOOST_FOREACH to iterate through hello and print each character
BOOST_FOREACH(
char ch, hello )
{
cout << ch;
}

// declare an array with 10 elements
int arr[10]={0,1,2,3,4,5,6,7,8,9};
//we can either print the array using for loop like this:
cout<<endl<<"Printing the array using for loop : "<<endl;
for (int i=0;i<10;i++)
{
cout<<i<<" ";
}
// or, we can use the BOOST_FOREACH to print the array like :
cout<<endl<<"Printing the array using BOOST_FOREACH : "<<endl;
BOOST_FOREACH(
int i,arr)
{
cout<<i<<" ";
}
// we can also process the container elements inside the block
// like if we want to double up each element,we will do it using this block :
cout<<endl<<"Multiplying the elements by 2 and printing them using BOOST_FOREACH"<<endl;
BOOST_FOREACH(
int i,arr)
{
i*=
2;
cout<<i<<" ";
}
cout<<endl;
// BOOST_FOREACH can iterate through any sequence available
// we can also iteratre through a sequence to process the
// container elements

         // we declare a string x with a value "boordbqdsr". If we increase every alphabet
// by one, it'd result in cppsecrets
string x("boordbqdsr");
cout<<"The initial string is : "<<x<<endl;
cout<<"String after incrementing each alphabet by one : ";
BOOST_FOREACH(
int i,x)
{
cout<<char(i+1); // we typecast the output+1
}
cout<<endl;

// declaring a vector v
vector<int> v{100,200,300,400,500};
cout<<"Printing the vector after dividing each element by 100 : ";
BOOST_FOREACH(
int i,v)
{
i/=
100;
cout<<i<<" ";
}
cout<<endl;
// there is a concept of BOOST_REVERSE_FOREACH which will iterate through the container
// in reverse and we can do the processing starting from the last element of the container
cout<<"Printing the array in reverse : ";
BOOST_REVERSE_FOREACH(
int i,arr)
{
cout<<i<<" ";
}
cout<<endl<<"Printing cppsecrets in reverse using BOOST_REVERSE_FOREACH : ";
// to print cppsecrets in reverse, we print it by incrementing each
// element as we did before:
BOOST_REVERSE_FOREACH(
int i,x)
{
cout<<char(i+1);
}
return 0;
}


Output:
Hello, world! Printing the array using for loop : 0 1 2 3 4 5 6 7 8 9 Printing the array using BOOST_FOREACH : 0 1 2 3 4 5 6 7 8 9 Multiplying the elements by 2 and printing them using BOOST_FOREACH 0 2 4 6 8 10 12 14 16 18 The initial string is : boordbqdsr String after incrementing each alphabet by one : cppsecrets Printing the vector after dividing each element by 100 : 1 2 3 4 5 Printing the array in reverse : 9 8 7 6 5 4 3 2 1 0 Printing cppsecrets in reverse using BOOST_REVERSE_FOREACH : stercesppc

More Articles of Ayush Ranjan:

Name Views Likes
C++ boost::type_traits::type_with_alignment 231 10
C++ boost::type_traits::type_identity 234 9
C++ boost::type_traits::remove_volatile 198 12
C++ boost::type_traits::remove_reference 223 11
C++ boost::type_traits::remove_pointer 211 5
C++ boost::type_traits::remove_extent 214 4
C++ boost::type_traits::remove_cv 239 6
C++ boost::type_traits::remove_const 226 15
C++ boost::type_traits::remove_all_extents 235 9
C++ boost::type_traits::rank 194 5
C++ boost::type_traits::promote 224 21
C++ boost::type_traits::make_unsigned 216 21
C++ boost::type_traits::make_signed 212 4
C++ boost::type_traits::is_volatile 223 1
C++ boost::type_traits::is_void 208 9
C++ boost::type_traits::is_virtual_base_of 207 1
C++ boost::type_traits::is_unsigned 190 14
C++ boost::type_traits::make_signed 77 9
C++ boost::type_traits::is_union 226 11
C++ boost::type_traits::is_stateless 195 3
C++ boost::type_traits::is_signed 202 9
C++ boost::type_traits::is_scalar 193 13
C++ boost::type_traits::is_same 221 0
C++ boost::type_traits::is_rvalue_reference 212 9
C++ boost::type_traits::is_reference 203 0
C++ boost::type_traits::is_polymorphic 193 8
C++ boost::type_traits::is_pointer 190 19
C++ boost::type_traits::is_pod 201 17
C++ boost::type_traits::is_object 185 6
C++ boost::type_traits::is_nothrow_move_constructible 226 25
C++ boost::type_traits::is_nothrow_move_assignable 201 8
C++ boost::type_traits::is_member_pointer 187 14
C++ boost::type_traits::is_member_object_pointer 191 15
C++ boost::type_traits::is_member_function_pointer 203 14
C++ boost::type_traits::is_lvalue_reference 191 7
C++ boost::type_traits::is_integral 206 15
C++ boost::type_traitsis_fundamental 195 12
C++ boost::type_traits::is_function 201 17
C++ boost::type_traits::is_floating_point 199 6
C++ boost::type_traits::is_final 195 6
C++ boost::type_traits::is_enum 197 11
C++ boost::type_traits::is_empty 228 4
C++ boost::type_traits::is_destructible 202 1
C++ boost::type_traits::is_default_constructible 199 2
C++ boost::type_traits::is_copy_constructible 210 13
C++ boost::type_traits::is_copy_assignable 204 7
C++ boost::type_traits::is_convertible 207 4
C++ boost::type_traits::is_constructible 206 1
C++ boost::type_traits::is_const 228 1
C++ boost::type_traits::is_compound 208 1
C++ boost::type_traits::is_complex 276 8
C++ boost::type_traits::is_class 212 14
C++ boost::type_traits::is_base_of 203 17
C++ boost::type_traits::is_assignable 202 33
C++ boost::type_traits::is_array 210 23
C++ boost::Type_traits::is_arithmetic 215 21
C++ boost::type_traits::is_abstract 211 18
C++ boost::type_traits::integral_promotion 199 4
C++ boost::type_traits::integral_constant 188 34
C++ boost::type_traits::has_virtual_destructor 180 5
C++ boost::type_traits::has_unary_plus 198 8
C++ boost::type_traits::has_unary_minus 216 7
C++ boost::type_traits::has_trivial_move_constructor 166 6
C++ boost::type_traits::has_trivial_move_assign 182 8
C++ boost::type_traits::has_trivial_destructor 179 23
C++ boost::type_traits::has_unary_plus 190 9
C++ boost::type_traits::has_unary_minus 149 10
C++ boost::type_traits::has_trivial_copy 138 4
C++ boost::type_traits::has_trivial_constructor 166 4
C++ boost::type_traits::has_trivial_assign 146 3
C++ boost::type_traits::has_right_shift_assign 115 10
C++ boost::type_traits::has_right_shift 146 5
C++ boost::type_traits::has_pre_increment 124 10
C++ boost::type_traits::has_pre_decrement 114 0
C++ boost::type_traits::has_post_increment 108 4
C++ boost::type_traits::has_post_decrement 148 56
C++ boost::type_traits::has_plus_assign 131 4
C++ boost::type_traits::has_plus 134 21
C++ boost::type_traits::has_nothrow_destructor 116 5
C++ boost::type_traits::has_nothrow_copy 128 6
C++ boost::type_traits::has_nothrow_constructor 143 3
C++ boost::type_traits::has_nothrow_assgin 146 6
C++ boost::type_traits::has_not_equal_to 153 2
C++ boost::type_traits::has_new_operator 128 24
C++ boost::type_traits::has_negate 145 35
C++ boost::type_traits::has_multiplies_assign 135 46
C++ boost::type_traits::has_multiplies 141 16
C++ boost::type_traits::has_modulus_assign 125 3
C++ boost::type_traits::has_modulus 148 2
C++ boost::type_traits::has_minus_assign 138 2
C++ boost::type_traits::has_minus 116 2
C++ boost::type_traits::has_logical_or 133 1
C++ boost::type_traits::has_logical_not 125 1
C++ boost::type_traits::has_logical_and 132 1
C++ boost::type_traits::has_less_equal 112 1
C++ boost::type_traits::has_less 109 1
C++ boost::type_traits::left_shift_assign 132 1
C++ boost::type_traits::has_left_shift 111 1
C++ boost::type_traits::has_greater_equal 116 1
C++ boost::type_traits::has_greater 115 1
C++ boost::type_traits::has_equal_to 120 1
C++ boost::type_traits::has_divides_assign 113 1
C++ boost::type_traits::has_divides 108 1
C++ boost::type_traits::has_dereference 120 1
C++ boost::type_traits::has_complement 109 1
C++ boost::type_traits::has_bit_xor_assign 127 1
C++ boost::type_traits::has_bit_xor 120 1
C++ boost::type_traits::has_bit_or_assign 124 1
C++ boost::type_traits::has_bit_or 152 1
C++ boost::type_traits::has_bit_and_assign 122 3
C++ boost::type_traits::has_bit_and 114 1
C++ boost::type_traits::function_traits 236 1
C++ boost::type_traits::floating_point_promotion 116 1
C++ boost::type_traits::extent 132 1
C++ boost::type_traits::declval 130 1
C++ boost::type_traits::decay 108 1
C++ boost::type_traits::copy_cv 122 1
C++ boost::type_traits::common_type 123 1
C++ boost::type_traits::conditional 129 1
C++ boost::type_traits::alignment_of 153 1
C++ boost::type_traits::aligned_storage 122 1
C++ boost::type_traits::add_volatile 147 1
C++ boost::type_traits::add_rvalue_reference 128 1
C++ boost::type_traits::add_reference 138 1
C++ boost::type_traits::add_pointer 137 1
C++ boost::type_traits::add_lvalue_reference 123 1
C++ boost::type_traits::add_cv 140 1
C++ boost::type_traits::add_const 144 1
C++boost::EnableIf 220 2
C++ boost::foreach 453 1
C++ boost::lambda 273 1
C++ Boost Lambda 326 1
Program to check whether a number is Palindrome or not 221 2

Comments