Note that there are some explanatory texts on larger screens.

plurals
  1. POIterating over the output of a member function in std::for_each
    text
    copied!<p>I have a class with an accessor member function that I want to call and apply the result to a functor using std::for_each. I have a working version below that uses a for loop and for_each, but the for_each version is cryptic and cumbersome. Is there a way I can make the for_each version more concise, considering I have access to boost, but not C++11?</p> <pre><code>#if 0 // for loop version: for(value_vector_type::iterator it = values.begin(); it!=values.end(); it++){ avg(it-&gt;getValue()); // I want to put this in a for_each loop } #else // bind version: std::for_each(values.begin(), values.end(), // iterate over all values boost::bind( boost::mem_fn(&amp;average_type::operator()), // attach the averaging functor to the output of the getvalue call &amp;avg, boost::bind( boost::mem_fn(&amp;value_wrapper_type::getValue), // bind the getValue call to each element in values _1 ) ) ); #endif </code></pre> <p>Here is the full working implementation:</p> <pre><code>#include &lt;vector&gt; #include &lt;algorithm&gt; #include &lt;iostream&gt; #include &lt;boost/bind.hpp&gt; #include &lt;boost/bind/mem_fn.hpp&gt; // A value wrapper template&lt;typename T&gt; struct Value { Value(){} Value(const T&amp; value, bool valid = true):m_value(value),m_valid(valid){} T getValue(){ return m_value; } bool getValid(){ return m_valid; } void setValue(const T&amp; value){ m_value = value; } void setValid(const T&amp; valid){ m_valid = valid; } private: T m_value; bool m_valid; }; // Class that calculates the average piecewise template&lt;typename T&gt; struct Average { private: T m_numPoints; T m_ChannelSum; public: Average() : m_numPoints(0), m_ChannelSum(0.0){} void operator()(T value){ m_numPoints++; m_ChannelSum+=value; } double getAverage(){ return m_ChannelSum/m_numPoints; } T getCount(){ return m_numPoints; } T getSum(){ return m_ChannelSum; } }; // Run the average computation on several values int main(int argc, char** argv){ typedef int value_type; typedef Value&lt;value_type&gt; value_wrapper_type; typedef std::vector&lt;value_wrapper_type&gt; value_vector_type; value_vector_type values; values.push_back(value_wrapper_type(5)); values.push_back(value_wrapper_type(7)); values.push_back(value_wrapper_type(3)); values.push_back(value_wrapper_type(1)); values.push_back(value_wrapper_type(2)); typedef Average&lt;value_type&gt; average_type; average_type avg; #if 0 // for loop version: for(value_vector_type::iterator it = values.begin(); it!=values.end(); it++){ avg(it-&gt;getValue()); // I want to put this in a for_each loop } #else // bind version: std::for_each(values.begin(), values.end(), // iterate over all values boost::bind( boost::mem_fn(&amp;average_type::operator()), // attach the averaging functor to the output of the getvalue call &amp;avg, boost::bind( boost::mem_fn(&amp;value_wrapper_type::getValue), // bind the getValue call to each element in values _1 ) ) ); #endif std::cout &lt;&lt; "Average: " &lt;&lt; avg.getAverage() &lt;&lt; " Count: " &lt;&lt; avg.getCount() &lt;&lt; " Sum: " &lt;&lt; avg.getSum() &lt;&lt; std::endl; } </code></pre> <p>note: my original question was how to construct a for_each at all, but I've found that solution and a whole new question did not make much sense.</p> <p>Thanks, all help is really appreciated!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload