g*********s 发帖数: 1782 | 1 【 以下文字转载自 JobHunting 讨论区 】
发信人: qqcheerup (qq), 信区: JobHunting
标 题: C++ template Questions
发信站: BBS 未名空间站 (Sun Jan 16 22:00:26 2011, 美东)
有1道题请教大家
1. 实现 Reduce function using templates. The Reduce fn applies a function
of two arguments cumulatively to the items of an STL container, from
begin() to end(), so as to reduce the sequence to a single value. For
example, Reduce(, std::plus()) should
calculate ((((1+2)+3)+4)+5).
class NotEnoughElements {};
template
typename Container::value_type
Reduce(const Container& c, Function fn) throw (NotEnoughElements)
{
实现
} | t****t 发帖数: 6806 | 2 and your question is?
【在 g*********s 的大作中提到】 : 【 以下文字转载自 JobHunting 讨论区 】 : 发信人: qqcheerup (qq), 信区: JobHunting : 标 题: C++ template Questions : 发信站: BBS 未名空间站 (Sun Jan 16 22:00:26 2011, 美东) : 有1道题请教大家 : 1. 实现 Reduce function using templates. The Reduce fn applies a function : of two arguments cumulatively to the items of an STL container, from : begin() to end(), so as to reduce the sequence to a single value. For : example, Reduce(, std::plus()) should : calculate ((((1+2)+3)+4)+5).
| S*********g 发帖数: 5298 | 3 这个好像是interactive brokers的面试题。
我过了他们的这个written test来着。
1000伪币,我把我的答案卖给你。哈哈哈哈
【在 g*********s 的大作中提到】 : 【 以下文字转载自 JobHunting 讨论区 】 : 发信人: qqcheerup (qq), 信区: JobHunting : 标 题: C++ template Questions : 发信站: BBS 未名空间站 (Sun Jan 16 22:00:26 2011, 美东) : 有1道题请教大家 : 1. 实现 Reduce function using templates. The Reduce fn applies a function : of two arguments cumulatively to the items of an STL container, from : begin() to end(), so as to reduce the sequence to a single value. For : example, Reduce(, std::plus()) should : calculate ((((1+2)+3)+4)+5).
| r*******m 发帖数: 109 | 4 template
typename Container::value_type
Reduce(const Container& c, Function fn)
{
if (c.size()<1) throw NotEnoughElement();
typename Container::value_type T(0);
for (typename Container::const_iterator it=c.begin();it!=c.end();++it)
T=fn(T,*it);
return T;
}
【在 g*********s 的大作中提到】 : 【 以下文字转载自 JobHunting 讨论区 】 : 发信人: qqcheerup (qq), 信区: JobHunting : 标 题: C++ template Questions : 发信站: BBS 未名空间站 (Sun Jan 16 22:00:26 2011, 美东) : 有1道题请教大家 : 1. 实现 Reduce function using templates. The Reduce fn applies a function : of two arguments cumulatively to the items of an STL container, from : begin() to end(), so as to reduce the sequence to a single value. For : example, Reduce(, std::plus()) should : calculate ((((1+2)+3)+4)+5).
| q*******p 发帖数: 39 | 5 Thanks
but how do you known function has two parameters? | r*******m 发帖数: 109 | 6 that's in the original question. binary functor. If you need to check input
function, thats more complicated since it's compiler time issue. Given
unirory functor won't work in compiler time.
【在 q*******p 的大作中提到】 : Thanks : but how do you known function has two parameters?
| g*********s 发帖数: 1782 | 7 what's the best answer.
【在 t****t 的大作中提到】 : and your question is?
| g*********s 发帖数: 1782 | 8 what if there's no "zero" defined for type T?
it=c.begin();it!=c.end();++it)
【在 r*******m 的大作中提到】 : template : typename Container::value_type : Reduce(const Container& c, Function fn) : { : if (c.size()<1) throw NotEnoughElement(); : typename Container::value_type T(0); : for (typename Container::const_iterator it=c.begin();it!=c.end();++it) : T=fn(T,*it); : return T; : }
| t****t 发帖数: 6806 | 9 you are not supposed to use 0 as initial value, as the question states you
are supposed to reduce(1, 2, 3, 4, 5) to ((((1, 2), 3), 4), 5), as opposed
to (((((0, 1), 2), 3), 4), 5)
【在 r*******m 的大作中提到】 : template : typename Container::value_type : Reduce(const Container& c, Function fn) : { : if (c.size()<1) throw NotEnoughElement(); : typename Container::value_type T(0); : for (typename Container::const_iterator it=c.begin();it!=c.end();++it) : T=fn(T,*it); : return T; : }
| r*******m 发帖数: 109 | 10 as someone said, you can start it+1, initialize the T with the first element
. small change of code.
【在 g*********s 的大作中提到】 : what if there's no "zero" defined for type T? : : it=c.begin();it!=c.end();++it)
| | | e********e 发帖数: 250 | 11 Meyer said "avoid using size(), use empty()". | g*********s 发帖数: 1782 | 12 oh, yes. when func is *, reduction from 0 actually is wrong.
you
opposed
【在 t****t 的大作中提到】 : you are not supposed to use 0 as initial value, as the question states you : are supposed to reduce(1, 2, 3, 4, 5) to ((((1, 2), 3), 4), 5), as opposed : to (((((0, 1), 2), 3), 4), 5)
| g*********s 发帖数: 1782 | 13 can't use empty() bah. should be:
if ( size() < 2) throw NotEnoughElements.
【在 e********e 的大作中提到】 : Meyer said "avoid using size(), use empty()".
| e********e 发帖数: 250 | 14 1 element is okay, it is the result. even if one element is not allowed,
avoid using size() as it is slow for list, do things like it = c.begin(), it
!= c.end() && ++it != c.end().
【在 g*********s 的大作中提到】 : can't use empty() bah. should be: : if ( size() < 2) throw NotEnoughElements.
| e****d 发帖数: 895 | 15 function body()
{
if (c.begin() == c.end())
throw NotEnoughElements();
Container::const_iterator iter = c.begin();
return std::accumulate(iter, c.end(), *iter++, fn);
}
【在 g*********s 的大作中提到】 : 【 以下文字转载自 JobHunting 讨论区 】 : 发信人: qqcheerup (qq), 信区: JobHunting : 标 题: C++ template Questions : 发信站: BBS 未名空间站 (Sun Jan 16 22:00:26 2011, 美东) : 有1道题请教大家 : 1. 实现 Reduce function using templates. The Reduce fn applies a function : of two arguments cumulatively to the items of an STL container, from : begin() to end(), so as to reduce the sequence to a single value. For : example, Reduce(, std::plus()) should : calculate ((((1+2)+3)+4)+5).
| t****t 发帖数: 6806 | 16 this looks good but unfortunately is undefined. you can't use iter and *iter
++ like this in one expression.
【在 e****d 的大作中提到】 : function body() : { : if (c.begin() == c.end()) : throw NotEnoughElements(); : Container::const_iterator iter = c.begin(); : return std::accumulate(iter, c.end(), *iter++, fn); : }
| e****d 发帖数: 895 | 17 Shouldn't parameters are evaluated (even though the order is undefined)
before the function body is called including copying parameters?
iter
【在 t****t 的大作中提到】 : this looks good but unfortunately is undefined. you can't use iter and *iter : ++ like this in one expression.
| e****d 发帖数: 895 | 18 Oh, actually, it's true the undefined order causes undefined values
are passed in.
【在 e****d 的大作中提到】 : Shouldn't parameters are evaluated (even though the order is undefined) : before the function body is called including copying parameters? : : iter
| t****t 发帖数: 6806 | 19 it's guaranteed that parameters are evaluated before function. however, as i
said, iter and *iter++ can not be evaluated simutaneously.
【在 e****d 的大作中提到】 : Shouldn't parameters are evaluated (even though the order is undefined) : before the function body is called including copying parameters? : : iter
| t****t 发帖数: 6806 | 20 no no, there is no such concept as "undefined values" in c++. there is
undefined behaviour, and there is indetermined value.
【在 e****d 的大作中提到】 : Oh, actually, it's true the undefined order causes undefined values : are passed in.
| e****d 发帖数: 895 | 21 That's right. Should be "indetermined value", which
causes "undefined behaviour".
【在 t****t 的大作中提到】 : no no, there is no such concept as "undefined values" in c++. there is : undefined behaviour, and there is indetermined value.
|
|