I have a set of pairs defined as follow:
set< pair<UserEquipment*, double> > 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 5 7723
cesco sade: I have a set of pairs defined as follow:
set< pair<UserEquipment*, double> > 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 <class InputIterator, class T, class BinaryOperation>
T accumulate (InputIterator first, InputIterator last, T init,
BinaryOperation binary_op);
Where foreach element:
init = binary_op(init,*first);
--
TB @ SWEDEN
In article <11**********************@z14g2000cwz.googlegroups .com>,
"cesco" <fd**********@gmail.com> wrote: I have a set of pairs defined as follow:
set< pair<UserEquipment*, double> > 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<std::pair<Object*, double> > MySet;
MySet mySet;
double unit = accumulate(mySet.begin(), mySet.end(), 0.0,
plus_second<MySet::value_type>() );
}
The 'plus_second' functor is defined as follows:
template <typename Pair>
struct plus_second :
std::binary_function<typename Pair::second_type, Pair,
typename Pair::second_type>
{
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<std::pair<Object*, double> > MySet;
MySet mySet;
double unit = accumulate(mySet.begin(), mySet.end(), 0.0,
apply_to_second<std::plus<double>, 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<std::pair<Object*, double> > 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.
cesco wrote: I have a set of pairs defined as follow:
set< pair<UserEquipment*, double> > 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
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<pair<UserEquipment*, double> SetOfFsDouble;
// SetOfFsDouble numberOfFreqSlotsToAdd
transform(numberOfFreqSlotsToAdd.begin(), numberOfFreqSlotsToAdd.end(),
numberOfFreqSlotsToAdd.begin(), f_gx(bind2nd(std::multiplies<double>(),
unit), select2nd<SetOfFsDouble::value_type>()));
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<SetOfFsDouble::value_type>() ,
(bind2nd(std::multiplies<double>(), unit),
select2nd<SetOfFsDouble::value_type>())) );
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<std::_Tree<_Traits>::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<std::_Tset_traits<std::pair<User Equipment
*,double>,SinrBasedWithoutPowerScaling::NumberOfFr equencySlotsComparison,std::allocator<std::pair<Us erEquipment
*,double>>,false>>::iterator,
1> _Traits=std::_Tset_traits<std::pair<UserEquipment
*,double>,SinrBasedWithoutPowerScaling::NumberOfFr equencySlotsComparison,std::allocator<std::pair<Us erEquipment
*,double>>,false>,
1> _Fn1=std::pair<select1st<std::pair<UserEquipment
*,double>>,select2nd<std::pair<UserEquipment *,double>>>,
1>
_Cat1=std::_Tree<std::_Tset_traits<std::pair<UserE quipment
*,double>,SinrBasedWithoutPowerScaling::NumberOfFr equencySlotsComparison,std::allocator<std::pair<Us erEquipment
*,double>>,false>>::iterator::iterator_category,
1>
_Cat2=std::_Tree<std::_Tset_traits<std::pair<UserE quipment
*,double>,SinrBasedWithoutPowerScaling::NumberOfFr equencySlotsComparison,std::allocator<std::pair<Us erEquipment
*,double>>,false>>::iterator::iterator_category,
1>
_InIt=std::_Tree<std::_Tset_traits<std::pair<UserE quipment
*,double>,SinrBasedWithoutPowerScaling::NumberOfFr equencySlotsComparison,std::allocator<std::pair<Us erEquipment
*,double>>,false>>::iterator,
1>
_InOutItCat=std::_Iter_random_helper<std::_Tree<st d::_Tset_traits<std::pair<UserEquipment
*,double>,SinrBasedWithoutPowerScaling::NumberOfFr equencySlotsComparison,std::allocator<std::pair<Us erEquipment
*,double>>,false>>::iterator::iterator_category,st d::_Tree<std::_Tset_traits<std::pair<UserEquipment
*,double>,SinrBasedWithoutPowerScaling::NumberOfFr equencySlotsComparison,std::allocator<std::pair<Us erEquipment
*,double>>,false>>::iterator::iterator_category>:: _Iter_random_cat
1> ]
Do you have any suggestion on how to solve this problem?
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Christof Krueger |
last post by:
Hello,
I'm quite new to C++ so maybe there's something I miss.
I write a simple board game. It has a board class. This class has a
method that returns the count of pieces a player has on the...
|
by: tuko |
last post by:
Hello kind people.
I have the classes "point", "curve", "surface", "region" as shown
below. Every class has a member function that returns a list
of pointers to objects that comprise the *this...
|
by: yinglcs |
last post by:
I have a function in STL which add the 'area'
attribute of a list of Rect.
template< class T1, class T2> class add_area_per_cent
: public binary_function<T2, T1, T2>
{
public:...
|
by: aka_eu |
last post by:
I have this problem.
I'm trying to get all the valid combination C(N,K) that pass one
condition.
For example C(5,3) can use in any combination this numbers {1,2,3,4,5}
But let's say that I...
|
by: Henrique A. Menarin |
last post by:
I want to create a function _and so that I could write
vector<boolasserts;
//...
bool b = _and(asserts);
and it "anded" all the elements in the vector. I implemented it like
this:
|
by: Drunkard |
last post by:
Hallo does anyone have shortest path with forbidden pairs algorithm written in C or C++ or .net or C# ?????? Please help me.
|
by: striker16 |
last post by:
Hi there,
I'm looking for an algorithm to solve following problem:
There is a random list of floating point numbers with two positions after the decimal point (dollar prices).
Any two of those...
|
by: mcjason |
last post by:
I saw something interesting about a grid pair puzzle problem that
it looks like a machine when you find each that work out the way it
does and say with all the others that work out the way they...
|
by: shashankbs |
last post by:
Given a topology and a certain node X, find the shortest path tree
with X as the root.
* Input: a topology file similar to the following (which is a three-node
ring)
...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |