Boost Range Test

//
// Difference between iterator_range and range_iterator
//
void testBoostRange()
{
typedef int Array[20];
cout << typeid( range_iterator<Array>::type ).name() << endl;

Array arr={11,22,33,44,55,66,77,88};
// Important to note that we use the boost for_each version of this algorithm
// otherwise it wouldn’t work!!!
boost::for_each( make_iterator_range(arr,arr+5) ,print());

// string test
char text[10]=”a,b,c,d”;
std::string w_myStr=”jean”;

// declare a range iterator (forward iterator)
boost::iterator_range<char*> w_chrRange(text);
int* w_myInt=new int[5];
auto w_test=boost::make_iterator_range( w_myInt, w_myInt+5);

// the range iterator concept (helper to retrieve information about the range iterator)
// not sure of the use of the range iterator (template bug fixed “>>”)
typedef boost::range_iterator<boost::iterator_range<int*>>::type iter_type;
typedef boost::range_value<boost::iterator_range<int*>>::type value_type;
typedef boost::range_difference<boost::iterator_range<int*>>::type diff_type;
// check range distance
diff_type w_testRDiff = std::distance( w_test.begin(), w_test.end());
BOOST_ASSERT( w_testRDiff==5);

TestStruct w_jeanTest(5); //
// create an iterator range
auto jeanRange=boost::make_iterator_range( w_jeanTest.begin(), w_jeanTest.end());
// create a function that we initialize to a lambda function
std::function<void(int)> w_justAfunc=[](int i) {std::cout << “Container element: ” << i << “\n”;};

// try to create a function from std::bind
// MyStruct w_testBindFunc;
// std::function<void(void)> w_bindFunc=std::bind( std::mem_fn( &MyStruct::printf),&w_testBindFunc,std::placeholders::_1);
TestStruct w_jbStruct(5);
std::find_if( w_jbStruct.begin(), w_jbStruct.end(),
std::bind( &TestStruct::isEven, &w_jbStruct, std::placeholders::_1));

for_each( jeanRange.begin(), jeanRange.end(), w_justAfunc); // std version
for_each( make_iterator_range( w_jeanTest.begin(),w_jeanTest.end()), w_justAfunc); // boost version

std::size_t w_aa=w_test.size();
std::for_each( w_test.begin(),w_test.end(),
[](int i) {std::cout << “Print range element: ” << i << “\n”;});
}

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *