435,426 Members | 3,152 Online
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

# Using accumulate algorithm with a set of pairs

 P: n/a I have a set of pairs defined as follow: set< pair > numberOfFreqSlotsToAdd; and I need to iterate through all the elements of the set in order accumulate the second field of the pair (that is double). Is there any way I can use the algorithm accumulate to accomplish this? The following line of code double unit = accumulate( numberOfFreqSlotsToAdd.begin(), numberOfFreqSlotsToAdd.end(), 0.0); of course, won't work. Can you suggest anything similar using the algorithm accumulate? Thanks Francesco Feb 12 '06 #1
5 Replies

 P: n/a cesco sade: I have a set of pairs defined as follow: set< pair > numberOfFreqSlotsToAdd; and I need to iterate through all the elements of the set in order accumulate the second field of the pair (that is double). Is there any way I can use the algorithm accumulate to accomplish this? The following line of code double unit = accumulate( numberOfFreqSlotsToAdd.begin(), numberOfFreqSlotsToAdd.end(), 0.0); of course, won't work. Can you suggest anything similar using the algorithm accumulate? template T accumulate (InputIterator first, InputIterator last, T init, BinaryOperation binary_op); Where foreach element: init = binary_op(init,*first); -- TB @ SWEDEN Feb 12 '06 #2

 P: n/a In article <11**********************@z14g2000cwz.googlegroups .com>, "cesco" wrote: I have a set of pairs defined as follow: set< pair > numberOfFreqSlotsToAdd; and I need to iterate through all the elements of the set in order accumulate the second field of the pair (that is double). Is there any way I can use the algorithm accumulate to accomplish this? The following line of code double unit = accumulate( numberOfFreqSlotsToAdd.begin(), numberOfFreqSlotsToAdd.end(), 0.0); of course, won't work. Can you suggest anything similar using the algorithm accumulate? I gave an answer to this in comp.learn.c-c++. Here I will give several answers: class Object { }; int main() { typedef std::set > MySet; MySet mySet; double unit = accumulate(mySet.begin(), mySet.end(), 0.0, plus_second() ); } The 'plus_second' functor is defined as follows: template struct plus_second : std::binary_function { typename Pair::second_type operator()( const typename Pair::second_type& x, const Pair& y ) const { return x + y.second; } }; Or something slightly more generic: class Object { }; int main() { typedef std::set > MySet; MySet mySet; double unit = accumulate(mySet.begin(), mySet.end(), 0.0, apply_to_second, MySet::value_type>()); } Which uses something like (though I can't say I like the name much:) template < typename Op, typename Pair > class apply_to_second : std::binary_function< typename Pair::second_type, Pair, typename Pair::second_type > { Op fn; public: typename Pair::second_type operator()( const typename Pair::second_type& x, const Pair& y ) const { return fn( x, y.second ); } }; Whether you use one of the above, the one I gave in the other group, or the simplest one: class Object { }; typedef std::set > MySet; double fn(double x, const MySet::value_type& y) { return x + y.second; } int main() { MySet mySet; double unit = accumulate(mySet.begin(), mySet.end(), 0.0, ptr_fun(&fn)); } Is up to you. I'll leave you with the XP mantra, "use the simplest thing that works, refactor mercilessly." -- Magic depends on tradition and belief. It does not welcome observation, nor does it profit by experiment. On the other hand, science is based on experience; it is open to correction by observation and experiment. Feb 13 '06 #3

 P: n/a cesco wrote: I have a set of pairs defined as follow: set< pair > numberOfFreqSlotsToAdd; and I need to iterate through all the elements of the set in order accumulate the second field of the pair (that is double). Is there any way I can use the algorithm accumulate to accomplish this? The following line of code double unit = accumulate( numberOfFreqSlotsToAdd.begin(), numberOfFreqSlotsToAdd.end(), 0.0); of course, won't work. Can you suggest anything similar using the algorithm accumulate? You can use an iterator that gets a first or second pair's member for you. http://groups.google.com/group/comp....b344960588d3ac Feb 13 '06 #4

 P: n/a I'm facing a similar problem: I need to transform the set of pairs by scaling the second element (that is a double) by a certain factor. The following line of code: // typedef set SetOfFsDouble; // SetOfFsDouble numberOfFreqSlotsToAdd transform(numberOfFreqSlotsToAdd.begin(), numberOfFreqSlotsToAdd.end(), numberOfFreqSlotsToAdd.begin(), f_gx(bind2nd(std::multiplies(), unit), select2nd())); reports the following error: 1>c:\program files\microsoft visual studio 8\vc\include\algorithm(650) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'double' (or there is no acceptable conversion) 1> c:\program files\microsoft visual studio 8\vc\include\utility(55): could be 'std::pair<_Ty1,_Ty2> &std::pair<_Ty1,_Ty2>::operator =(const std::pair<_Ty1,_Ty2> &)' 1> with 1> [ 1> _Ty1=UserEquipment *, 1> _Ty2=double 1> ] 1> while trying to match the argument list '(std::pair<_Ty1,_Ty2>, double)' 1> with 1> [ 1> _Ty1=UserEquipment *, 1> _Ty2=double 1> ] while the following approach: transform(numberOfFreqSlotsToAdd.begin(), numberOfFreqSlotsToAdd.end(), numberOfFreqSlotsToAdd.begin(), ::make_pair(select1st() , (bind2nd(std::multiplies(), unit), select2nd())) ); where I try to overcome the previous problem by creating a Pair to give back to the set, reports the following error: 1>c:\program files\microsoft visual studio 8\vc\include\algorithm(650) : error C2064: term does not evaluate to a function taking 1 arguments 1> c:\program files\microsoft visual studio 8\vc\include\algorithm(685) : see reference to function template instantiation '_OutIt std::_Transform::iterator,_Out It,_Fn1,std::_Iter_random_helper<_Cat1,_Cat2>::_It er_random_cat>(_InIt,_InIt,_OutIt,_Fn1,_InOutItCat ,std::_Range_checked_iterator_tag)' being compiled 1> with 1> [ 1> _OutIt=std::_Tree,SinrBasedWithoutPowerScaling::NumberOfFr equencySlotsComparison,std::allocator>,false>>::iterator, 1> _Traits=std::_Tset_traits,SinrBasedWithoutPowerScaling::NumberOfFr equencySlotsComparison,std::allocator>,false>, 1> _Fn1=std::pair>,select2nd>>, 1> _Cat1=std::_Tree,SinrBasedWithoutPowerScaling::NumberOfFr equencySlotsComparison,std::allocator>,false>>::iterator::iterator_category, 1> _Cat2=std::_Tree,SinrBasedWithoutPowerScaling::NumberOfFr equencySlotsComparison,std::allocator>,false>>::iterator::iterator_category, 1> _InIt=std::_Tree,SinrBasedWithoutPowerScaling::NumberOfFr equencySlotsComparison,std::allocator>,false>>::iterator, 1> _InOutItCat=std::_Iter_random_helper,SinrBasedWithoutPowerScaling::NumberOfFr equencySlotsComparison,std::allocator>,false>>::iterator::iterator_category,st d::_Tree,SinrBasedWithoutPowerScaling::NumberOfFr equencySlotsComparison,std::allocator>,false>>::iterator::iterator_category>:: _Iter_random_cat 1> ] Do you have any suggestion on how to solve this problem? Feb 13 '06 #5

 P: n/a cesco wrote: I'm facing a similar problem: I need to transform the set of pairs by scaling the second element (that is a double) by a certain factor. Use boost::transform_iterator for that. Feb 13 '06 #6

### This discussion thread is closed

Replies have been disabled for this discussion.