Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
Difficulty:
5/10
Date
added: 29th of December 2009
In
this exercise, you learn to replace for-loops by algorithms.
Reading
the literature, one reads:
Prefer
algorithms over loops [0][1].
This
is easier said than done.
In
this exercise you must replace for-loops by using a combination of all those
algorithm things like std::for_each, std::transform, std::bind1st,
std::bind2nd, std::multiplies
and more of the likes. It is up to you to find the correct combination.
The
exercises are unordered. Some require Boost, but
will be in namespace std
after the C++0x standard.
(C++) Exercise #9: No for-loops
Question #4:
Widget::DoIt on Widget
Question #5:
Widget::DoItOften on Widget
Question #6:
Widget::DoIt on Widget*
Question #7:
Widget::DoItOften on Widget*
Question #8: Sum a
std::vector<int>
Question #9: Product
of std::vector<int>
Question #10:
Widget::DoIt on boost::shared_ptr<Widget>
Question #11: Replace
zero by one
Question #12: Replace
negative by zero
Question #15: write
to std::cout
Replace
the for-loop. You will need:
View the answer of this exercise
Replace
the for-loop. You will need:
const std::vector<int>
AddTwo(const std::vector<int>& v)
{
std::vector<int> v_new(v); //Copy original vector
{
v_new[i]+=2;
}
return
v_new;
}
View the answer of this exercise
Replace
the for-loop. You will need:
View the answer of this exercise
Replace
the for-loop. You will need:
const std::vector<int>
Add(const std::vector<int>& v, const
int x)
{
std::vector<int> v_new(v); //Copy original vector
{
v_new[i]+=x;
}
return
v_new;
}
View the answer of this exercise
Replace
the for-loop. You will need:
*
std::mem_fun_ref (or boost::mem_fn)
struct Widget
{
void
DoIt() const { /* do it */ }
};
void DoIt(const std::vector<Widget>&
v)
{
{
v[i].DoIt();
}
}
View the answer of this exercise
Replace
the for-loop. You will need:
* std::bind2nd (or boost::bind)
* std::mem_fun_ref
(or boost::mem_fn)
struct Widget
{
void
DoItOften(const int
n) const { /* do it n times */ }
};
void DoItOften(const std::vector<Widget>& v, const int n)
{
{
v[i].DoItOften(n);
}
}
View the answer of this exercise
Replace
the for-loop. You will need:
* std::mem_fun (or boost::mem_fn)
struct Widget
{
void
DoIt() const { /* do it */ }
};
void DoIt(const std::vector<Widget*>&
v)
{
{
v[i]->DoIt();
}
}
View the answer of this exercise
Replace
the for-loop. You will need:
* std::bind2nd (or boost::bind)
* std::mem_fun (or boost::mem_fn)
struct Widget
{
void
DoItOften(const int
n) const { /* do it n times */ }
};
void DoItOften(const std::vector<Widget*>& v, const int n)
{
{
v[i]->DoItOften(n);
}
}
View the answer of this exercise
Replace
the for-loop. You will need:
View the answer of this exercise
Replace
the for-loop. You will need:
View the answer of this exercise
Replace
the for-loop. You will need:
#include <boost/shared_ptr.hpp>
struct Widget
{
void
DoIt() const { /* do it */ }
};
void DoIt(const boost::shared_ptr<std::vector<Widget> >& v)
{
{
v[i]->DoIt();
}
}
View the answer of this exercise
Replace
the for-loop. You will need:
* std::replace (or std::replace_if with std::bind2nd)
View the answer of this exercise
Replace
the for-loop. You will need:
View the answer of this exercise
Replace
the for-loop. You will need:
* your own std::unary_function
View the answer of this exercise
Replace
the for-loop. You will need:
* your own std::unary_function
View the answer of this exercise
Replace
the for-loop. You will need:
View the answer of this exercise
Replace
the for-loop. You will need:
View the answer of this exercise
Replace
the for-loop. You will need:
View the answer of this exercise
Feel
free to post
your feedback about this exercise at Programmer's Heaven.
[0]
Bjarne Stroustrup. The C++
Programming Language (3rd edition). ISBN: 0-201-88954-4. Chapter 18.12.1 :
'Prefer algorithms over loops'
[1]
Herb Sutter and Andrei Alexandrescu. C++ coding
standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6.
Chapter 84: 'Prefer algorithm calls to handwritten loops.'
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.