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::remove_volatile 597 12
C++ boost::type_traits::is_object 488 6
C++ boost::type_traits::is_destructible 481 1
C++ boost::type_traits::has_nothrow_copy 363 6
C++ boost::type_traits::is_const 458 1
C++boost::EnableIf 604 2
C++ boost::type_traits::has_virtual_destructor 449 5
C++ boost::type_traits::remove_extent 510 4
C++ boost::type_traits::has_post_increment 363 4
C++ boost::type_traits::extent 403 1
C++ boost::type_traits::is_enum 562 11
C++ boost::type_traits::has_unary_minus 479 7
C++ boost::type_traits::has_minus 346 2
C++ boost::type_traits::is_member_pointer 515 14
C++ boost::type_traits::has_divides_assign 322 1
C++ boost::type_traits::has_divides 339 1
C++ boost::type_traits::has_bit_or_assign 383 1
C++ boost::type_traits::has_bit_or 436 1
C++ boost::type_traits::has_equal_to 381 1
C++ boost::type_traits::has_nothrow_constructor 445 3
C++ boost::type_traits::has_negate 398 35
C++ boost::type_traits::has_trivial_move_assign 485 8
C++ boost::type_traits::has_pre_increment 539 10
C++ boost::type_traits::is_virtual_base_of 515 1
C++ boost::type_traits::is_member_object_pointer 504 15
C++ boost::type_traits::remove_reference 482 11
C++ boost::type_traits::rank 427 5
C++ boost::type_traits::is_union 616 11
C++ boost::type_traits::has_unary_plus 408 9
C++ boost::type_traits::remove_const 458 15
C++ boost::type_traits::is_unsigned 509 14
C++ boost::type_traits::add_lvalue_reference 364 1
C++ boost::type_traits::has_less 358 1
C++ boost::type_traitsis_fundamental 428 12
C++ boost::type_traits::has_trivial_destructor 445 23
C++ boost::lambda 712 1
C++ boost::type_traits::decay 393 1
C++ boost::type_traits::copy_cv 406 1
C++ boost::type_traits::aligned_storage 358 1
C++ boost::type_traits::is_member_function_pointer 486 14
C++ boost::type_traits::add_pointer 397 1
C++ boost::type_traits::has_bit_xor 372 1
C++ boost::type_traits::remove_pointer 502 5
C++ boost::type_traits::has_bit_xor_assign 339 1
C++ boost::type_traits::add_rvalue_reference 363 1
C++ boost::type_traits::make_signed 463 4
C++ boost::type_traits::promote 527 21
C++ boost::type_traits::has_bit_and_assign 324 3
C++ boost::type_traits::common_type 379 1
C++ boost::type_traits::is_same 503 0
C++ boost::type_traits::is_rvalue_reference 550 9
C++ boost::type_traits::has_plus 383 21
C++ boost::foreach 1498 1
C++ boost::type_traits::is_compound 472 1
C++ boost::type_traits::add_const 400 1
C++ boost::type_traits::add_cv 493 1
C++ boost::type_traits::has_greater_equal 382 1
C++ boost::type_traits::remove_all_extents 507 9
C++ boost::type_traits::is_array 543 23
C++ boost::type_traits::function_traits 732 1
C++ boost::type_traits::has_nothrow_destructor 367 5
C++ boost::type_traits::has_plus_assign 366 4
C++ boost::type_traits::make_signed 77 9
C++ boost::type_traits::is_base_of 454 17
C++ boost::type_traits::has_post_decrement 418 56
C++ boost::type_traits::is_constructible 510 1
C++ boost::type_traits::has_dereference 337 1
C++ boost::type_traits::is_volatile 500 1
C++ Boost Lambda 699 1
C++ boost::type_traits::has_not_equal_to 399 2
C++ boost::type_traits::is_function 466 17
C++ boost::type_traits::is_abstract 499 18
C++ boost::type_traits::type_with_alignment 569 10
C++ boost::type_traits::has_multiplies 473 16
C++ boost::type_traits::is_final 465 6
C++ boost::type_traits::type_identity 539 9
C++ boost::type_traits::is_assignable 431 33
C++ boost::type_traits::has_greater 334 1
C++ boost::type_traits::has_unary_plus 413 8
C++ boost::type_traits::has_trivial_assign 373 3
C++ boost::type_traits::is_default_constructible 438 2
C++ boost::Type_traits::is_arithmetic 459 21
C++ boost::type_traits::is_complex 766 8
C++ boost::type_traits::has_logical_and 358 1
C++ boost::type_traits::has_pre_decrement 314 0
C++ boost::type_traits::has_left_shift 328 1
C++ boost::type_traits::alignment_of 432 1
C++ boost::type_traits::has_trivial_constructor 407 4
C++ boost::type_traits::has_modulus 357 2
C++ boost::type_traits::is_copy_assignable 395 7
C++ boost::type_traits::is_polymorphic 491 8
C++ boost::type_traits::is_signed 559 9
C++ boost::type_traits::make_unsigned 513 21
C++ boost::type_traits::integral_constant 402 34
C++ boost::type_traits::is_pointer 481 19
C++ boost::type_traits::is_pod 484 17
C++ boost::type_traits::is_integral 587 15
C++ boost::type_traits::has_right_shift_assign 321 10
C++ boost::type_traits::remove_cv 548 6
C++ boost::type_traits::has_logical_not 322 1
C++ boost::type_traits::integral_promotion 399 4
C++ boost::type_traits::has_minus_assign 328 2
C++ boost::type_traits::is_nothrow_move_constructible 472 25
C++ boost::type_traits::declval 372 1
C++ boost::type_traits::has_multiplies_assign 386 46
C++ boost::type_traits::add_volatile 359 1
C++ boost::type_traits::is_nothrow_move_assignable 469 8
C++ boost::type_traits::is_scalar 520 13
C++ boost::type_traits::is_void 522 9
C++ boost::type_traits::is_empty 438 4
Program to check whether a number is Palindrome or not 481 2
C++ boost::type_traits::has_right_shift 377 5
C++ boost::type_traits::left_shift_assign 414 1
C++ boost::type_traits::has_logical_or 436 1
C++ boost::type_traits::add_reference 422 1
C++ boost::type_traits::has_modulus_assign 338 3
C++ boost::type_traits::is_floating_point 459 6
C++ boost::type_traits::is_convertible 444 4
C++ boost::type_traits::is_lvalue_reference 444 7
C++ boost::type_traits::has_new_operator 360 24
C++ boost::type_traits::has_less_equal 328 1
C++ boost::type_traits::has_trivial_move_constructor 368 6
C++ boost::type_traits::is_reference 477 0
C++ boost::type_traits::has_complement 277 1
C++ boost::type_traits::has_bit_and 283 1
C++ boost::type_traits::floating_point_promotion 317 1
C++ boost::type_traits::has_unary_minus 320 10
C++ boost::type_traits::has_nothrow_assgin 416 6
C++ boost::type_traits::has_trivial_copy 315 4
C++ boost::type_traits::is_class 469 14
C++ boost::type_traits::is_copy_constructible 439 13
C++ boost::type_traits::conditional 298 1
C++ boost::type_traits::is_stateless 437 3

Comments